Last active
February 20, 2021 17:25
-
-
Save silkyland/c7e0849d90d13a3f4c64f23abfdc6b15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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