Updated dashboard

This commit is contained in:
m.razaghi
2026-08-03 17:32:04 +03:30
parent aad799d29f
commit 74ead9d0ed
2 changed files with 40 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ use App\Models\Suggestion;
use App\Models\Ticket; use App\Models\Ticket;
use App\Models\UserSubscriber; use App\Models\UserSubscriber;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
class DashboardController extends Controller class DashboardController extends Controller
{ {
@@ -16,22 +17,32 @@ class DashboardController extends Controller
// 1. Calculate General Statistics // 1. Calculate General Statistics
$totalUsers = User::count(); $totalUsers = User::count();
// Using your UserSubscriber model, checking the is_free column and expired_at date // 2. Safely Count Free Subscribers using the relationship
$paidSubscribers = UserSubscriber::where('expired_at', '>=', now())
->where('is_free', false)
->distinct('user_id')
->count('user_id');
$freeSubscribers = UserSubscriber::where('expired_at', '>=', now()) $freeSubscribers = UserSubscriber::where('expired_at', '>=', now())
->where('is_free', true) ->whereHas('subscribe', function (Builder $query) {
$query->where('is_free', true);
})
->distinct('user_id') ->distinct('user_id')
->count('user_id'); ->count('user_id');
// 2. Fetch Recent Data (Limit to 5) // 3. Get Paid Subscribers & Breakdown by Plan Type
$paidBreakdown = UserSubscriber::where('user_subscribers.expired_at', '>=', now())
->whereHas('subscribe', function (Builder $query) {
$query->where('is_free', false);
})
->join('subscribe_plans', 'user_subscribers.subscribe_plan_id', '=', 'subscribe_plans.id')
->selectRaw('subscribe_plans.name as plan_name, count(DISTINCT user_subscribers.user_id) as total')
->groupBy('subscribe_plans.name')
->get();
// Total paid is the sum of the breakdown
$paidSubscribers = $paidBreakdown->sum('total');
// 4. Fetch Recent Data (Limit to 5)
$recentSuggestions = Suggestion::with('user')->latest()->take(5)->get(); $recentSuggestions = Suggestion::with('user')->latest()->take(5)->get();
$recentTickets = Ticket::with('user')->latest('updated_at')->take(5)->get(); $recentTickets = Ticket::with('user')->latest('updated_at')->take(5)->get();
// 3. Prepare Chart Data (Last 7 Days) // 5. Prepare Chart Data (Last 7 Days)
$chartDates = []; $chartDates = [];
$newUsersData = []; $newUsersData = [];
$paidSubsData = []; $paidSubsData = [];
@@ -39,19 +50,22 @@ class DashboardController extends Controller
for ($i = 6; $i >= 0; $i--) { for ($i = 6; $i >= 0; $i--) {
$date = now()->subDays($i); $date = now()->subDays($i);
$chartDates[] = $date->format('Y-m-d'); // Example: "2023-10-25" $chartDates[] = $date->format('Y-m-d');
$dbDate = $date->format('Y-m-d'); $dbDate = $date->format('Y-m-d');
// Line Chart Data: New Users
$newUsersData[] = User::whereDate('created_at', $dbDate)->count(); $newUsersData[] = User::whereDate('created_at', $dbDate)->count();
// Column Chart Data: Subscriptions // Note: If you want to see all-time data in the chart instead of just 7 days,
$paidSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)->where('is_free', false)->count(); // you would need to group by month instead of day.
$freeSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)->where('is_free', true)->count(); $paidSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)
->whereHas('subscribe', fn($q) => $q->where('is_free', false))->count();
$freeSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)
->whereHas('subscribe', fn($q) => $q->where('is_free', true))->count();
} }
return view('admin.dashboard.index', compact( return view('admin.dashboard.index', compact(
'totalUsers', 'paidSubscribers', 'freeSubscribers', 'totalUsers', 'paidSubscribers', 'freeSubscribers', 'paidBreakdown',
'recentSuggestions', 'recentTickets', 'recentSuggestions', 'recentTickets',
'chartDates', 'newUsersData', 'paidSubsData', 'freeSubsData' 'chartDates', 'newUsersData', 'paidSubsData', 'freeSubsData'
)); ));

View File

@@ -25,7 +25,17 @@
<div class="d-flex"> <div class="d-flex">
<div class="flex-grow-1"> <div class="flex-grow-1">
<p class="text-white-50 font-weight-medium mb-2">اشتراک‌های فعال (پولی)</p> <p class="text-white-50 font-weight-medium mb-2">اشتراک‌های فعال (پولی)</p>
<h4 class="text-white mb-0">{{ number_format($paidSubscribers) }}</h4> <h4 class="text-white mb-2">{{ number_format($paidSubscribers) }}</h4>
<!-- Breakdown of Paid Plans -->
<div>
@foreach($paidBreakdown as $plan)
<span class="badge bg-light text-success font-size-12 me-1 mb-1 shadow-sm">
{{ $plan->plan_name }}: {{ $plan->total }}
</span>
@endforeach
</div>
</div> </div>
<div class="mini-stat-icon avatar-sm rounded-circle bg-white align-self-center"> <div class="mini-stat-icon avatar-sm rounded-circle bg-white align-self-center">
<span class="avatar-title bg-white text-success"><i class="mdi mdi-cash-multiple font-size-24"></i></span> <span class="avatar-title bg-white text-success"><i class="mdi mdi-cash-multiple font-size-24"></i></span>