Spaces:
Sleeping
Sleeping
File size: 1,325 Bytes
10dc6f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Event;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ReportController extends Controller
{
public function index()
{
// Monthly revenue (last 6 months)
$monthlyRevenue = Order::where('status', 'paid')
->where('paid_at', '>=', now()->subMonths(6))
->select(
DB::raw("DATE_FORMAT(paid_at, '%Y-%m') as month"),
DB::raw('SUM(total) as revenue'),
DB::raw('COUNT(*) as order_count')
)
->groupBy('month')
->orderBy('month')
->get();
// Top events by revenue
$topEvents = Event::withSum(['orders as revenue' => fn($q) => $q->where('status', 'paid')], 'total')
->withCount(['orders as paid_orders' => fn($q) => $q->where('status', 'paid')])
->orderByDesc('revenue')
->take(10)
->get();
// Order status breakdown
$statusBreakdown = Order::select('status', DB::raw('COUNT(*) as count'))
->groupBy('status')
->pluck('count', 'status');
return view('admin.reports', compact('monthlyRevenue', 'topEvents', 'statusBreakdown'));
}
}
|