Kasir/app/Http/Controllers/NampanController.php
2025-08-27 16:32:02 +07:00

89 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Nampan;
use Illuminate\Http\Request;
class NampanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return response()->json(
Nampan::withCount('items')->get()
);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'nama' => 'required|string|max:100',
],
[
'nama' => 'Nama nampan harus diisi.'
]);
Nampan::create($validated);
return response()->json([
'message' => 'Nampan berhasil dibuat'
],201);
}
/**
* Display the specified resource.
*/
public function show(int $id)
{
return response()->json(
Nampan::with('items')->find($id)
);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, int $id)
{
$validated = $request->validate([
'nama' => 'required|string|max:100',
],
[
'nama' => 'Nama nampan harus diisi.'
]);
$nampan = Nampan::findOrFail($id);
$nampan->update($validated);
return response()->json([
'message' => 'Nampan berhasil diupdate'
],200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(int $id)
{
$nampan = Nampan::findOrFail($id);
$nampan->items()->each(function ($item) {
$item->update(['id_nampan' => null]);
});
$nampan->delete();
return response()->json([
'message' => 'Nampan berhasil dihapus'
], 204);
}
}