Files
perpustakaanku/app/Http/Controllers/AnggotaController.php

77 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Anggota;
use Illuminate\Http\Request;
use Inertia\Inertia;
class AnggotaController extends Controller
{
public function index()
{
$anggotas = Anggota::latest()->paginate(10);
return Inertia::render('Anggota/Index', [
'anggotas' => $anggotas,
]);
}
public function create()
{
return Inertia::render('Anggota/Create');
}
public function store(Request $request)
{
$validated = $request->validate([
'nama' => 'required|string|max:255',
'email' => 'required|email|unique:anggotas',
'no_telepon' => 'nullable|string|max:15',
'alamat' => 'nullable|string',
'tanggal_bergabung' => 'required|date',
'status' => 'required|in:aktif,nonaktif',
]);
Anggota::create($validated);
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil ditambahkan!');
}
public function show(Anggota $anggota)
{
return Inertia::render('Anggota/Show', [
'anggota' => $anggota->load('peminjamans.buku'),
]);
}
public function edit(Anggota $anggota)
{
return Inertia::render('Anggota/Edit', [
'anggota' => $anggota,
]);
}
public function update(Request $request, Anggota $anggota)
{
$validated = $request->validate([
'nama' => 'required|string|max:255',
'email' => 'required|email|unique:anggotas,email,' . $anggota->id,
'no_telepon' => 'nullable|string|max:15',
'alamat' => 'nullable|string',
'tanggal_bergabung' => 'required|date',
'status' => 'required|in:aktif,nonaktif',
]);
$anggota->update($validated);
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil diupdate!');
}
public function destroy(Anggota $anggota)
{
$anggota->delete();
return redirect()->route('anggota.index')->with('success', 'Anggota berhasil dihapus!');
}
}