diff --git a/app/Http/Controllers/Admin/AkunController.php b/app/Http/Controllers/Admin/AkunController.php new file mode 100644 index 0000000..2740876 --- /dev/null +++ b/app/Http/Controllers/Admin/AkunController.php @@ -0,0 +1,96 @@ +get(); + return view('admin.akun.index', compact('users')); + } + + public function create() + { + $gurus = Guru::whereNull('user_id')->get(['id', 'nama_guru']); + $waliMurids = WaliMurid::whereNull('user_id')->get(['id', 'nama_wali']); + + return view('admin.akun.create', compact('gurus', 'waliMurids')); + } + + public function store(Request $request) + { + $request->validate([ + 'role' => 'required|in:guru,wali_murid', + 'user_id' => 'required', + ]); + + if ($request->role == 'guru') { + $data = Guru::findOrFail($request->user_id); + $name = $data->nama_guru; + $email = $data->email ?? strtolower(str_replace(' ', '', $data->nama_guru)) . '@paud.local'; + $data = WaliMurid::findOrFail($request->user_id); + $name = $data->nama_wali; + $email = $data->email ?? strtolower(str_replace(' ', '', $data->nama_wali)) . '@paud.local'; + } + + if (User::where('email', $email)->exists()) { + return back()->with('error', 'Email sudah digunakan untuk akun lain.'); + } + + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'password' => Hash::make('123456'), + 'role' => $request->role, + ]); + + $data->update(['user_id' => $user->id]); + + return redirect()->route('akun.index')->with('success', 'Akun berhasil dibuat!'); + } + + public function destroy(User $akun) + { + $akun->delete(); + return redirect()->route('akun.index')->with('success', 'Akun berhasil dihapus.'); + } + + public function edit(User $akun) + { + return view('admin.akun.edit', compact('akun')); + } + + public function update(Request $request, User $akun) + { + $request->validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|email|unique:users,email,' . $akun->id, + 'role' => 'required|in:admin,guru,wali_murid', + ]); + + $akun->update([ + 'name' => $request->name, + 'email' => $request->email, + 'role' => $request->role, + ]); + + return redirect()->route('akun.index')->with('success', 'Akun berhasil diperbarui.'); + } + + public function resetPassword(User $akun) + { + $akun->update([ + 'password' => Hash::make('123456'), + ]); + + return redirect()->route('akun.index')->with('success', 'Password berhasil direset ke default (123456).'); + } +} diff --git a/app/Models/Guru.php b/app/Models/Guru.php index d5f234b..c83a833 100644 --- a/app/Models/Guru.php +++ b/app/Models/Guru.php @@ -8,12 +8,20 @@ class Guru extends Model { use HasFactory; + + // kasih tahu Laravel kalau nama tabelnya "guru", bukan "gurus" protected $table = 'guru'; - + protected $fillable = [ - 'nama_guru', - 'email', + 'nama', + 'jenis_guru', 'no_hp', - 'bidang', + 'email', + 'user_id', ]; + + public function user() + { + return $this->belongsTo(User::class); + } } \ No newline at end of file diff --git a/app/Models/WaliMurid.php b/app/Models/WaliMurid.php index b153333..0e0f087 100644 --- a/app/Models/WaliMurid.php +++ b/app/Models/WaliMurid.php @@ -13,14 +13,14 @@ class WaliMurid extends Model protected $fillable = [ 'nama_wali', - 'email', 'no_hp', + 'email', 'alamat', + 'user_id', ]; - // Tambahin relasi ke siswa - public function siswas() + public function user() { - return $this->hasMany(Siswa::class, 'wali_id'); + return $this->belongsTo(User::class); } } \ No newline at end of file diff --git a/resources/views/admin/akun/create.blade.php b/resources/views/admin/akun/create.blade.php new file mode 100644 index 0000000..8971404 --- /dev/null +++ b/resources/views/admin/akun/create.blade.php @@ -0,0 +1,54 @@ +@extends('layouts.app') + +@section('content') +
| # | +Nama | +Role | +Password | +Aksi | +|
|---|---|---|---|---|---|
| {{ $i + 1 }} | +{{ $user->name }} | +{{ $user->email }} | ++ @if($user->role == 'guru') + Guru + @elseif($user->role == 'wali_murid') + Wali Murid + @else + Admin + @endif + | + + {{-- Kolom password (disembunyikan tapi bisa dilihat) --}} +
+
+
+
+
+ |
+
+ {{-- Tombol Aksi --}}
+ + Edit + + + + + | +
| Belum ada akun terdaftar. | +|||||
Menu Akun