API Kategori
materi 7user_3285855
php
2 years ago
2.4 kB
10
Indexable
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Kategori; use Illuminate\Support\Facades\Validator; class KategoriController extends Controller { /** * Display a listing of the resource. */ public function index() { $kategori = Kategori::all(); return response()->json([ 'data' => $kategori, ]); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validator = Validator::make($request->only('nama'), [ 'nama' => 'required', ]); if ($validator->fails()) { return response()->json([ 'data' => $validator->errors(), 'message' => 'validasi gagal', ]); } $kategori = Kategori::create([ 'nama' => $request->nama, ]); return response()->json([ 'data' => $kategori, 'message' => 'berhasil membuat data', ]); } /** * Display the specified resource. */ public function show(string $id) { // } /** * Show the form for editing the specified resource. */ public function edit(string $id) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Kategori $kategori) { $validator = Validator::make($request->only('nama'), [ 'nama' => 'required', ]); if ($validator->fails()) { return response()->json([ 'data' => $validator->errors(), 'message' => 'validasi gagal test', ]); } $kategori->update([ 'nama' => $request->nama, ]); return response()->json([ 'data' => $kategori, 'message' => 'berhasil update data', ]); } /** * Remove the specified resource from storage. */ public function destroy(Kategori $kategori) { $kategori->delete(); return response()->json([ 'data' => $kategori, 'message' => 'berhasil hapus data', ]); } }
Editor is loading...
Leave a Comment