diff --git a/app/Http/Controllers/Guru/DashboardController.php b/app/Http/Controllers/Guru/DashboardController.php index 84a76f9..1307187 100644 --- a/app/Http/Controllers/Guru/DashboardController.php +++ b/app/Http/Controllers/Guru/DashboardController.php @@ -4,6 +4,9 @@ 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 @@ -12,25 +15,32 @@ public function index() { $guru = Auth::guard('guru')->user(); - // 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) - $totalSiswa = Mengajar::where('nip', $guru->nip) - ->with('kelas.siswa') - ->get() - ->pluck('kelas.siswa') - ->flatten() - ->unique('nisn') - ->count(); + // 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')); } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/Guru/GuruController.php b/app/Http/Controllers/Guru/GuruController.php index 658ba7d..13c2d11 100644 --- a/app/Http/Controllers/Guru/GuruController.php +++ b/app/Http/Controllers/Guru/GuruController.php @@ -26,4 +26,4 @@ public function index(Request $request) return view('guru.guru.index', compact('gurus')); } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/Guru/LoginController.php b/app/Http/Controllers/Guru/LoginController.php index 858eb70..70babd2 100644 --- a/app/Http/Controllers/Guru/LoginController.php +++ b/app/Http/Controllers/Guru/LoginController.php @@ -1,7 +1,4 @@ validate([ 'nip' => 'required', @@ -36,6 +35,7 @@ public function loginGuru(Request $request) ])->withInput($request->except('password')); } + // Logout public function logout(Request $request) { Auth::guard('guru')->logout(); diff --git a/app/Http/Controllers/Guru/SiswaController.php b/app/Http/Controllers/Guru/SiswaController.php index f340636..c99439a 100644 --- a/app/Http/Controllers/Guru/SiswaController.php +++ b/app/Http/Controllers/Guru/SiswaController.php @@ -35,4 +35,4 @@ public function index(Request $request) return view('guru.siswa.index', compact('siswas', 'kelass')); } -} \ No newline at end of file +} diff --git a/app/Models/Guru.php b/app/Models/Guru.php index e6ffd1c..046252b 100644 --- a/app/Models/Guru.php +++ b/app/Models/Guru.php @@ -29,4 +29,4 @@ public function mengajars() { return $this->hasMany(Mengajar::class, 'nip', 'nip'); } -} \ No newline at end of file +} diff --git a/app/Models/Kelas.php b/app/Models/Kelas.php index 1d56693..b310993 100644 --- a/app/Models/Kelas.php +++ b/app/Models/Kelas.php @@ -20,9 +20,15 @@ class Kelas extends Model 'tingkat', ]; + // Relasi ke Siswa public function siswa() { return $this->hasMany(Siswa::class, 'id_kelas', 'id_kelas'); } + // Relasi ke Mengajar + public function mengajars() + { + return $this->hasMany(Mengajar::class, 'id_kelas', 'id_kelas'); + } } \ No newline at end of file diff --git a/resources/views/auth/login-guru.blade.php b/resources/views/auth/login-guru.blade.php index 23fa545..76589e8 100644 --- a/resources/views/auth/login-guru.blade.php +++ b/resources/views/auth/login-guru.blade.php @@ -1,3 +1,4 @@ + @extends('layouts.auth') @section('title', 'Login Guru') @@ -11,20 +12,37 @@ ← Kembali ke Landing Page + {{-- Alert Error --}} + @if($errors->any()) +