refactor: Update KategoriController to use route model binding and improve validation messages
This commit is contained in:
@@ -2,63 +2,79 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Kategori;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class KategoriController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
$kategoris = Kategori::withCount('bukus')
|
||||
->latest()
|
||||
->paginate(10);
|
||||
|
||||
return Inertia::render('Kategori/Index', [
|
||||
'kategoris' => $kategoris,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
return Inertia::render('Kategori/Create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$validated = $request->validate([
|
||||
'nama_kategori' => 'required|string|max:255|unique:kategoris',
|
||||
'deskripsi' => 'nullable|string',
|
||||
]);
|
||||
|
||||
Kategori::create($validated);
|
||||
|
||||
return redirect()->route('kategori.index')
|
||||
->with('success', 'Kategori berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
public function show(Kategori $kategori)
|
||||
{
|
||||
//
|
||||
return Inertia::render('Kategori/Show', [
|
||||
'kategori' => $kategori->load('bukus'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
public function edit(Kategori $kategori)
|
||||
{
|
||||
//
|
||||
return Inertia::render('Kategori/Edit', [
|
||||
'kategori' => $kategori,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
public function update(Request $request, Kategori $kategori)
|
||||
{
|
||||
//
|
||||
$validated = $request->validate([
|
||||
'nama_kategori' => 'required|string|max:255|unique:kategoris,nama_kategori,' . $kategori->id,
|
||||
'deskripsi' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$kategori->update($validated);
|
||||
|
||||
return redirect()->route('kategori.index')
|
||||
->with('success', 'Kategori berhasil diupdate!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
public function destroy(Kategori $kategori)
|
||||
{
|
||||
//
|
||||
// Cek apakah kategori masih memiliki buku
|
||||
if ($kategori->bukus()->count() > 0) {
|
||||
return redirect()->route('kategori.index')
|
||||
->with('error', 'Kategori tidak dapat dihapus karena masih memiliki buku!');
|
||||
}
|
||||
|
||||
$kategori->delete();
|
||||
|
||||
return redirect()->route('kategori.index')
|
||||
->with('success', 'Kategori berhasil dihapus!');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user