e72f68cd86
git commit "fix dashboard charts rendering and views data display"
59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ActivityLog;
|
|
use App\Models\Admin;
|
|
use App\Models\Flight;
|
|
use App\Models\Tour;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$adminsCount = Admin::count();
|
|
|
|
$usersCount = User::count();
|
|
|
|
$toursCount = Tour::count();
|
|
|
|
$flightsCount = Flight::count();
|
|
|
|
$tourViews = $this->getViewsForLast10Days(Tour::class, 'tour_views');
|
|
|
|
$flightViews = $this->getViewsForLast10Days(Flight::class, 'flight_views');
|
|
|
|
$logs = ActivityLog::with('admin')
|
|
->latest()
|
|
->take(5)
|
|
->get();
|
|
|
|
return view('admin.dashboard', compact(
|
|
'adminsCount',
|
|
'usersCount', 'toursCount', 'flightsCount', 'tourViews',
|
|
'flightViews', 'logs'
|
|
));
|
|
}
|
|
|
|
private function getViewsForLast10Days(string $model, string $cachePrefix)
|
|
{
|
|
$ids = $model::pluck('id');
|
|
|
|
return collect(range(9, 0))->map(function ($i) use ($ids, $cachePrefix) {
|
|
$date = now()->subDays($i)->format('Y-m-d');
|
|
|
|
$total = $ids->sum(function ($id) use ($cachePrefix, $date) {
|
|
return Cache::get("{$cachePrefix}:{$id}:{$date}", 0);
|
|
});
|
|
|
|
return [
|
|
'date' => verta($date)->format('m/d'),
|
|
'total' => (int) $total,
|
|
];
|
|
})->values();
|
|
}
|
|
}
|