89 lines
2.0 KiB
PHP
89 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Item;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ItemController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return response()->json(
|
|
Item::with('produk.foto','nampan')->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'id_produk' => 'required',
|
|
'id_nampan' => 'nullable'
|
|
],[
|
|
'id_produk' => 'Id produk tidak valid.',
|
|
]);
|
|
|
|
$item = Item::create($validated);
|
|
|
|
return response()->json([
|
|
'message' => 'Item berhasil dibuat',
|
|
'data' => $item
|
|
],201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(int $id)
|
|
{
|
|
$item = Item::with('produk.foto','nampan')->findOrFail($id);
|
|
return response()->json($item);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, int $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'id_produk' => 'required|exists:produks,id',
|
|
'id_nampan' => 'nullable|exists:nampans,id'
|
|
],[
|
|
'id_produk' => 'Id produk tidak valid.',
|
|
'id_nampan' => 'Id nampan tidak valid'
|
|
]);
|
|
|
|
$item = Item::findOrFail($id)->update($validated);
|
|
|
|
return response()->json([
|
|
'message' => 'Item berhasil diubah',
|
|
'data' => $item
|
|
],200);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(int $id)
|
|
{
|
|
Item::findOrFail($id)->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Item berhasil dihapus'
|
|
],200);
|
|
}
|
|
|
|
// custom methods
|
|
public function brankasItem(){
|
|
$items = Item::with('produk.foto','nampan')->whereNull('id_nampan')->belumTerjual()->get();
|
|
|
|
return response()->json($items);
|
|
}
|
|
}
|