MIF_E31230356/app/Http/Controllers/Guru/DashboardController.php

46 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers\Guru;
use App\Http\Controllers\Controller;
use App\Models\Mengajar;
use App\Models\Guru;
use App\Models\Kelas;
use App\Models\Siswa;
use Illuminate\Support\Facades\Auth;
class DashboardController extends Controller
{
public function index()
{
$guru = Auth::guard('guru')->user();
// Cek table mengajars ada data atau enggak
try {
// Hitung total kelas yang diajar
$totalKelas = Mengajar::where('nip', $guru->nip)
->distinct('id_kelas')
->count('id_kelas');
// Hitung total mapel yang diajar
$totalMapel = Mengajar::where('nip', $guru->nip)
->distinct('id_mapel')
->count('id_mapel');
// Hitung total siswa yang diajar (lewat kelas)
$kelasIds = Mengajar::where('nip', $guru->nip)
->pluck('id_kelas')
->unique();
$totalSiswa = Siswa::whereIn('id_kelas', $kelasIds)->count();
} catch (\Exception $e) {
// Kalau error (table kosong atau relasi belum ada), set default 0
$totalKelas = 0;
$totalMapel = 0;
$totalSiswa = 0;
}
return view('guru.dashboard', compact('totalKelas', 'totalMapel', 'totalSiswa'));
}
}