where('tanggal', '>=', now()->subMonths(6)) ->groupBy('month', DB::raw('MONTH(tanggal)')) ->orderBy(DB::raw('MONTH(tanggal)')) ->get(); // treatments memakai medicine_id. Nama obat diambil dari tabel medicines. $topObats = Treatment::query() ->join('medicines', 'treatments.medicine_id', '=', 'medicines.id') ->select( 'medicines.id', 'medicines.nama_obat', DB::raw('SUM(treatments.qty_used) as total_used') ) ->whereNotNull('treatments.medicine_id') ->groupBy('medicines.id', 'medicines.nama_obat') ->orderByDesc('total_used') ->limit(5) ->get(); $topComplaints = Treatment::select('keluhan', DB::raw('COUNT(*) as total')) ->whereNotNull('keluhan') ->where('keluhan', '!=', '') ->groupBy('keluhan') ->orderByDesc('total') ->limit(5) ->get(); return view('pages.dashboard.medical', compact('monthlyTrends', 'topObats', 'topComplaints')); } }