Files
besttechfrom.eu/app/Http/Controllers/AdminController.php
T

82 lines
2.3 KiB
PHP
Raw Normal View History

2026-02-14 17:20:50 +02:00
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\User;
use App\Models\Category;
use App\Models\Comment;
use App\Models\Upvote;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AdminController extends Controller
{
/**
* Display the admin dashboard with statistics.
*/
public function dashboard()
{
// General Stats
$stats = [
'total_users' => User::count(),
'total_products' => Product::count(),
'approved_products' => Product::approved()->count(),
'pending_products' => Product::pending()->count(),
'rejected_products' => Product::rejected()->count(),
'total_categories' => Category::count(),
'total_comments' => Comment::count(),
'total_upvotes' => Upvote::count(),
];
// User Growth (last 7 days)
$userGrowth = User::where('created_at', '>=', now()->subDays(7))
->selectRaw('DATE(created_at) as date, COUNT(*) as count')
->groupBy('date')
->orderBy('date')
->get();
// Product Growth (last 7 days)
$productGrowth = Product::where('created_at', '>=', now()->subDays(7))
->selectRaw('DATE(created_at) as date, COUNT(*) as count')
->groupBy('date')
->orderBy('date')
->get();
// Top Categories (by product count)
$topCategories = Category::withCount('products')
->orderBy('products_count', 'desc')
->limit(5)
->get();
// Recent Users
$recentUsers = User::with('roles')
->latest()
->limit(5)
->get();
// Recent Products
$recentProducts = Product::with(['user', 'category'])
->latest()
->limit(5)
->get();
// Most Upvoted Products
$topProducts = Product::approved()
->withCount('upvotes')
->orderBy('upvotes_count', 'desc')
->limit(5)
->get();
return view('admin.dashboard', compact(
'stats',
'userGrowth',
'productGrowth',
'topCategories',
'recentUsers',
'recentProducts',
'topProducts'
));
}
}