89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Buku;
|
|
use App\Models\Kategori;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class BukuController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$bukus = Buku::with('kategori')
|
|
->latest()
|
|
->paginate(10);
|
|
|
|
return Inertia::render('Buku/Index', [
|
|
'bukus' => $bukus,
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return Inertia::render('Buku/Create', [
|
|
'kategoris' => Kategori::all(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'kategori_id' => 'required|exists:kategoris,id',
|
|
'judul' => 'required|string|max:255',
|
|
'pengarang' => 'required|string|max:255',
|
|
'penerbit' => 'required|string|max:255',
|
|
'tahun_terbit' => 'required|digits:4',
|
|
'isbn' => 'required|string|max:20|unique:bukus',
|
|
'stok' => 'required|integer|min:0',
|
|
'sinopsis' => 'nullable|string',
|
|
]);
|
|
|
|
$validated['stok_tersedia'] = $validated['stok'];
|
|
|
|
Buku::create($validated);
|
|
|
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil ditambahkan!');
|
|
}
|
|
|
|
public function show(Buku $buku)
|
|
{
|
|
return Inertia::render('Buku/Show', [
|
|
'buku' => $buku->load('kategori', 'peminjamans.anggota'),
|
|
]);
|
|
}
|
|
|
|
public function edit(Buku $buku)
|
|
{
|
|
return Inertia::render('Buku/Edit', [
|
|
'buku' => $buku,
|
|
'kategoris' => Kategori::all(),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, Buku $buku)
|
|
{
|
|
$validated = $request->validate([
|
|
'kategori_id' => 'required|exists:kategoris,id',
|
|
'judul' => 'required|string|max:255',
|
|
'pengarang' => 'required|string|max:255',
|
|
'penerbit' => 'required|string|max:255',
|
|
'tahun_terbit' => 'required|digits:4',
|
|
'isbn' => 'required|string|max:20|unique:bukus,isbn,' . $buku->id,
|
|
'stok' => 'required|integer|min:0',
|
|
'sinopsis' => 'nullable|string',
|
|
]);
|
|
|
|
$buku->update($validated);
|
|
|
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil diupdate!');
|
|
}
|
|
|
|
public function destroy(Buku $buku)
|
|
{
|
|
$buku->delete();
|
|
return redirect()->route('buku.index')->with('success', 'Buku berhasil dihapus!');
|
|
}
|
|
}
|