From cd5cfb49a98028cfdd7bbe54b6effc0be93452d2 Mon Sep 17 00:00:00 2001 From: Saufi Date: Sat, 11 Jul 2026 03:18:07 +0800 Subject: [PATCH] Superadmin boleh padam pengguna MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Butang Padam dalam senarai pengguna (dengan pengesahan) - Tidak boleh padam akaun sendiri — dipapar '(akaun anda)' - Tidak boleh padam pengguna yang telah mencatat rekod kehadiran (disokong FK dicatat_oleh_user_id di peringkat DB) Co-Authored-By: Claude Fable 5 --- app/Http/Controllers/PenggunaController.php | 18 ++++++++++++++++++ resources/views/pengguna/index.blade.php | 17 +++++++++++++++++ routes/web.php | 1 + 3 files changed, 36 insertions(+) diff --git a/app/Http/Controllers/PenggunaController.php b/app/Http/Controllers/PenggunaController.php index 6242af8..ef584c1 100644 --- a/app/Http/Controllers/PenggunaController.php +++ b/app/Http/Controllers/PenggunaController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\KehadiranKtm; use App\Models\Sppm\Dun; use App\Models\User; use Illuminate\Http\Request; @@ -65,4 +66,21 @@ class PenggunaController extends Controller return redirect()->route('pengguna.index')->with('status', 'Pengguna baharu berjaya didaftarkan.'); } + + public function destroy(Request $request, User $pengguna) + { + if ($pengguna->id === $request->user()->id) { + return back()->withErrors(['pengguna' => 'Anda tidak boleh memadam akaun anda sendiri.']); + } + + if (KehadiranKtm::where('dicatat_oleh_user_id', $pengguna->id)->exists()) { + return back()->withErrors([ + 'pengguna' => "Pengguna {$pengguna->name} tidak boleh dipadam kerana telah mencatat rekod kehadiran.", + ]); + } + + $pengguna->delete(); + + return redirect()->route('pengguna.index')->with('status', "Pengguna {$pengguna->name} telah dipadam."); + } } diff --git a/resources/views/pengguna/index.blade.php b/resources/views/pengguna/index.blade.php index 7e67b3e..cbaccbb 100644 --- a/resources/views/pengguna/index.blade.php +++ b/resources/views/pengguna/index.blade.php @@ -10,6 +10,10 @@ + @if ($errors->any()) +
{{ $errors->first() }}
+ @endif + @if ($pengguna->isEmpty())

Tiada pengguna lagi.

@@ -23,6 +27,7 @@ E-mel Peranan DUN + Tindakan @@ -38,6 +43,18 @@ } }} {{ $p->dun ? $p->dun->code . ' — ' . $p->dun->nama : '-' }} + + @if ($p->id === auth()->id()) + (akaun anda) + @else +
+ @csrf + @method('DELETE') + +
+ @endif + @endforeach diff --git a/routes/web.php b/routes/web.php index fc6afaf..cd14bf9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -44,5 +44,6 @@ Route::middleware(['auth'])->group(function () { Route::get('/pengguna', [PenggunaController::class, 'index'])->name('pengguna.index'); Route::get('/pengguna/cipta', [PenggunaController::class, 'create'])->name('pengguna.create'); Route::post('/pengguna', [PenggunaController::class, 'store'])->name('pengguna.store'); + Route::delete('/pengguna/{pengguna}', [PenggunaController::class, 'destroy'])->name('pengguna.destroy'); }); });