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;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Kategori;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
|
||||||
class KategoriController extends Controller
|
class KategoriController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Display a listing of the resource.
|
|
||||||
*/
|
|
||||||
public function index()
|
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()
|
public function create()
|
||||||
{
|
{
|
||||||
//
|
return Inertia::render('Kategori/Create');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Store a newly created resource in storage.
|
|
||||||
*/
|
|
||||||
public function store(Request $request)
|
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!');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function show(Kategori $kategori)
|
||||||
* Display the specified resource.
|
|
||||||
*/
|
|
||||||
public function show(string $id)
|
|
||||||
{
|
{
|
||||||
//
|
return Inertia::render('Kategori/Show', [
|
||||||
|
'kategori' => $kategori->load('bukus'),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function edit(Kategori $kategori)
|
||||||
* Show the form for editing the specified resource.
|
|
||||||
*/
|
|
||||||
public function edit(string $id)
|
|
||||||
{
|
{
|
||||||
//
|
return Inertia::render('Kategori/Edit', [
|
||||||
|
'kategori' => $kategori,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function update(Request $request, Kategori $kategori)
|
||||||
* Update the specified resource in storage.
|
|
||||||
*/
|
|
||||||
public function update(Request $request, string $id)
|
|
||||||
{
|
{
|
||||||
//
|
$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!');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function destroy(Kategori $kategori)
|
||||||
* Remove the specified resource from storage.
|
|
||||||
*/
|
|
||||||
public function destroy(string $id)
|
|
||||||
{
|
{
|
||||||
//
|
// 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