Skip to content

Instantly share code, notes, and snippets.

@silkyland
Last active February 20, 2021 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silkyland/c7e0849d90d13a3f4c64f23abfdc6b15 to your computer and use it in GitHub Desktop.
Save silkyland/c7e0849d90d13a3f4c64f23abfdc6b15 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function apiIndex()
{
$categories = Category::all();
return response()->json($categories);
}
public function apiStore(Request $request)
{
$request->validate([
'name' => 'required:min:3',
]);
$category = new Category();
$category->name = $request->input('name');
$category->save();
return response()->json($category);
}
public function apiCategory($id)
{
$category = Category::find($id);
return response()->json($category);
}
public function apiUpdate(Request $request, $id)
{
$request->validate([
'name' => 'required|min:3',
]);
$category = Category::find($id);
$category->name = $request->input('name');
$category->save();
return response()->json($category);
}
public function apiDestroy($id)
{
$category = Category::find($id);
$category->delete();
return response()->json(['message' => 'Delete Category successfuly']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment