103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
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
|
|
{
|
|
public function index()
|
|
{
|
|
// 1. Calculate General Statistics
|
|
$totalUsers = User::count();
|
|
|
|
// 2. Safely Count Free Subscribers using the relationship
|
|
$freeSubscribers = UserSubscriber::where('expired_at', '>=', now())
|
|
->whereHas('subscribe', function (Builder $query) {
|
|
$query->where('is_free', true);
|
|
})
|
|
->distinct('user_id')
|
|
->count('user_id');
|
|
|
|
// 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();
|
|
|
|
// 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 (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 = [];
|
|
|
|
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();
|
|
|
|
$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',
|
|
'paidBreakdown',
|
|
'recentSuggestions',
|
|
'recentTickets',
|
|
'chartDates',
|
|
'newUsersData',
|
|
'paidSubsData',
|
|
'freeSubsData'
|
|
));
|
|
}
|
|
}
|