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); } }