diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 2d5c6cf..ddcf1f9 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -8,6 +8,7 @@ use App\Models\Suggestion; use App\Models\Ticket; use App\Models\UserSubscriber; use Illuminate\Http\Request; +use Illuminate\Database\Eloquent\Builder; class DashboardController extends Controller { @@ -16,22 +17,32 @@ class DashboardController extends Controller // 1. Calculate General Statistics $totalUsers = User::count(); - // Using your UserSubscriber model, checking the is_free column and expired_at date - $paidSubscribers = UserSubscriber::where('expired_at', '>=', now()) - ->where('is_free', false) - ->distinct('user_id') - ->count('user_id'); - + // 2. Safely Count Free Subscribers using the relationship $freeSubscribers = UserSubscriber::where('expired_at', '>=', now()) - ->where('is_free', true) + ->whereHas('subscribe', function (Builder $query) { + $query->where('is_free', true); + }) ->distinct('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(); $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 = []; $newUsersData = []; $paidSubsData = []; @@ -39,19 +50,22 @@ class DashboardController extends Controller for ($i = 6; $i >= 0; $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'); - // Line Chart Data: New Users $newUsersData[] = User::whereDate('created_at', $dbDate)->count(); - // Column Chart Data: Subscriptions - $paidSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)->where('is_free', false)->count(); - $freeSubsData[] = UserSubscriber::whereDate('created_at', $dbDate)->where('is_free', true)->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(); + + $freeSubsData[] = UserSubscriber::whereDate('created_at', $dbDate) + ->whereHas('subscribe', fn($q) => $q->where('is_free', true))->count(); } return view('admin.dashboard.index', compact( - 'totalUsers', 'paidSubscribers', 'freeSubscribers', + 'totalUsers', 'paidSubscribers', 'freeSubscribers', 'paidBreakdown', 'recentSuggestions', 'recentTickets', 'chartDates', 'newUsersData', 'paidSubsData', 'freeSubsData' )); diff --git a/resources/views/admin/dashboard/index.blade.php b/resources/views/admin/dashboard/index.blade.php index 438c1a0..e715396 100644 --- a/resources/views/admin/dashboard/index.blade.php +++ b/resources/views/admin/dashboard/index.blade.php @@ -25,7 +25,17 @@

اشتراک‌های فعال (پولی)

-

{{ number_format($paidSubscribers) }}

+

{{ number_format($paidSubscribers) }}

+ + +
+ @foreach($paidBreakdown as $plan) + + {{ $plan->plan_name }}: {{ $plan->total }} + + @endforeach +
+