select('service_id', DB::raw('count(*) as total'), DB::raw('SUM(status = "active") as active_count')) ->groupBy('service_id') ->get(); return response()->json($stats); } public function getStats() { // Query the unified warehouse table, grouping by the service configuration $stats = DB::table('subscription_services as s') ->leftJoin('unified_subscribers as u', 's.id', '=', 'u.service_id') ->select( 's.id', 's.name', DB::raw('COUNT(u.id) as total_subscribers'), // Use CASE WHEN for strict SQL compatibility DB::raw('SUM(CASE WHEN u.status = "active" THEN 1 ELSE 0 END) as active_subscribers') ) ->groupBy('s.id', 's.name') ->get(); // Calculate a quick summary for the top-level overview $grandTotal = $stats->sum('total_subscribers'); $grandActive = $stats->sum('active_subscribers'); return response()->json([ 'summary' => [ 'total' => $grandTotal, 'active' => $grandActive, 'inactive' => $grandTotal - $grandActive ], 'services' => $stats ]); } // app/Http/Controllers/DashboardController.php // app/Http/Controllers/DashboardController.php public function details(Request $request, $id) { $service = DB::table('subscription_services')->where('id', $id)->first(); if (!$service) { abort(404, 'Service not found.'); } $search = $request->input('search'); $subscribers = DB::table('unified_subscribers') ->where('service_id', $id) ->when($search, function($query, $search) { $query->where(function($q) use ($search) { $q->where('phone_number', 'like', "%{$search}%") ->orWhere('content_name', 'like', "%{$search}%") ->orWhere('status', 'like', "%{$search}%") ->orWhere('join_date', 'like', "%{$search}%"); }); }) ->orderBy('updated_at', 'desc') ->paginate(15); return view('dashboard.details', compact('service', 'subscribers')); } }