Files
ilycats/app/Http/Controllers/MedicalDashboardController.php
T

46 lines
1.4 KiB
PHP
Raw Normal View History

2026-02-08 15:16:35 +07:00
<?php
namespace App\Http\Controllers;
use App\Models\Treatment;
use Illuminate\Support\Facades\DB;
class MedicalDashboardController extends Controller
{
public function index()
{
$monthlyTrends = Treatment::select(
DB::raw('MONTHNAME(tanggal) as month'),
DB::raw('COUNT(*) as total')
)
->where('tanggal', '>=', now()->subMonths(6))
->groupBy('month', DB::raw('MONTH(tanggal)'))
->orderBy(DB::raw('MONTH(tanggal)'))
->get();
2026-09-23 12:45:02 +07:00
// 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')
2026-02-08 15:16:35 +07:00
->orderByDesc('total_used')
2026-09-23 12:45:02 +07:00
->limit(5)
2026-02-08 15:16:35 +07:00
->get();
$topComplaints = Treatment::select('keluhan', DB::raw('COUNT(*) as total'))
2026-09-23 12:45:02 +07:00
->whereNotNull('keluhan')
->where('keluhan', '!=', '')
2026-02-08 15:16:35 +07:00
->groupBy('keluhan')
->orderByDesc('total')
2026-09-23 12:45:02 +07:00
->limit(5)
2026-02-08 15:16:35 +07:00
->get();
2026-09-23 12:45:02 +07:00
return view('pages.dashboard.medical', compact('monthlyTrends', 'topObats', 'topComplaints'));
2026-02-08 15:16:35 +07:00
}
2026-09-23 12:45:02 +07:00
}