diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index ddcf1f9..f7aa374 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -40,23 +40,45 @@ class DashboardController extends Controller // 4. Fetch Recent Data (Limit to 5) $recentSuggestions = Suggestion::with('user')->latest()->take(5)->get(); + + // Order tickets by updated_at so newly replied tickets jump to the top $recentTickets = Ticket::with('user')->latest('updated_at')->take(5)->get(); - // 5. Prepare Chart Data (Last 7 Days) + // 5. Prepare Chart Data (Dynamic Dates Based on Actual Records) + + // Grab the 7 most recent unique dates where users actually registered + $recentDates = User::latest('created_at') + ->take(200) // Look through the last 200 users to find distinct dates + ->get() + ->pluck('created_at') + ->map(function ($date) { + // Ensure we get a standard Y-m-d string for querying + return $date ? $date->format('Y-m-d') : null; + }) + ->filter() + ->unique() + ->take(7) + ->sort() // Sort chronologically (oldest to newest) + ->values() + ->toArray(); + + // Fallback just in case the database is completely empty + if (empty($recentDates)) { + $recentDates = [now()->format('Y-m-d')]; + } + $chartDates = []; $newUsersData = []; $paidSubsData = []; $freeSubsData = []; - for ($i = 6; $i >= 0; $i--) { - $date = now()->subDays($i); - $chartDates[] = $date->format('Y-m-d'); - $dbDate = $date->format('Y-m-d'); + foreach ($recentDates as $dbDate) { + // Add the date to the chart labels + $chartDates[] = $dbDate; + // Count records for this specific exact date $newUsersData[] = User::whereDate('created_at', $dbDate)->count(); - // Note: If you want to see all-time data in the chart instead of just 7 days, - // you would need to group by month instead of day. $paidSubsData[] = UserSubscriber::whereDate('created_at', $dbDate) ->whereHas('subscribe', fn($q) => $q->where('is_free', false))->count(); @@ -65,9 +87,16 @@ class DashboardController extends Controller } return view('admin.dashboard.index', compact( - 'totalUsers', 'paidSubscribers', 'freeSubscribers', 'paidBreakdown', - 'recentSuggestions', 'recentTickets', - 'chartDates', 'newUsersData', 'paidSubsData', 'freeSubsData' + 'totalUsers', + 'paidSubscribers', + 'freeSubscribers', + 'paidBreakdown', + 'recentSuggestions', + 'recentTickets', + 'chartDates', + 'newUsersData', + 'paidSubsData', + 'freeSubsData' )); } }