83 lines
1.7 KiB
PHP
83 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Kategori;
|
|
use Illuminate\Http\Request;
|
|
|
|
class KategoriController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return response()->json(
|
|
Kategori::withCount('produk')->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'nama' => 'required|string|max:100',
|
|
],
|
|
[
|
|
'nama' => 'Nama kategori harus diisi.'
|
|
]);
|
|
|
|
Kategori::create($validated);
|
|
|
|
|
|
return response()->json([
|
|
'message' => 'Kategori berhasil dibuat'
|
|
],201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(int $id)
|
|
{
|
|
return response()->json(
|
|
Kategori::with('items.produk.foto')->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 Kategori harus diisi.'
|
|
]);
|
|
|
|
$Kategori = Kategori::findOrFail($id);
|
|
|
|
$Kategori->update($validated);
|
|
|
|
return response()->json([
|
|
'message' => 'Kategori berhasil diupdate'
|
|
],200);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(int $id)
|
|
{
|
|
Kategori::findOrFail($id)->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'Kategori berhasil dihapus'
|
|
], 204);
|
|
}
|
|
}
|