- Forums
- Linux Commands
- Laravel Add New API Controller - Create CRUD Request, Model apiResource()
This Page Contains information about Laravel Add New API Controller - Create CRUD Request, Model apiResource() By jj in category Linux Commands with 0 Replies. [5158], Last Updated: Mon Jun 24, 2024
jj
Sun Jun 11, 2023
0 Comments
323 Visits
The purpose of this post is to show how to add a new API resources. For this example, use posts.
1. Create a new controller.
( -r ) Resource Controllers : https://laravel.com/docs/10.x/controllers#resource-controllers
php artisan make:controller PostController -r
Example resource controller:
- https://www.webune.com/forums/laravel-two-commands-to-start-a-new-api-project.html
- https://www.digitalocean.com/community/tutorials/simple-laravel-crud-with-resource-controllers#storing-a-resource-store
2. add apiResource to routes/api.php
Route::apiResource('/posts', PostController::class);
3. Available routes with apiResource
Route |
Controller Method |
METHOD |
Description |
/posts |
index() |
GET |
Show all posts |
/posts |
create() |
POST |
Add new post |
|
|
|
|
/posts/{id} |
show() |
GET |
Show individual post |
/posts/{id} |
update() |
PUT |
Submitted form to update individual post |
/posts/{id} |
destroy() |
DELETE |
Delete individual post |
Verb |
URI |
Action |
Route Name |
GET |
/pictures |
index |
pictures.index |
GET |
/pictures/create |
create |
pictures.create |
POST |
/pictures |
store |
pictures.store |
GET |
/pictures/{picture} |
show |
pictures.show |
GET |
/pictures/{picture}/edit |
edit |
pictures.edit |
PUT/PATCH |
/pictures/{picture} |
update |
pictures.update |
DELETE |
/pictures/{picture} |
destroy |
pictures.destroy |
4. PostController.php
- index()
- create()
- store(Request $request)
- show($id)
- edit($id)
- update(Request $request, $id)
- destroy($id)
usage: Api.php
Route::apiResource('/tasks', SchoolTasksController::class);
example controller: SchoolTasksController
Route::apiResource('/tasks', SchoolTasksController::class);
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Resources\SchoolTasksResource;
use App\Http\Controllers\Controller;
use App\Http\Requests\SchoolTasksRequest;
use App\Models\SchoolTasks;
use Illuminate\Support\Facades\Auth;
// https://dev.to/tanzimibthesam/making-api-crud-create-read-update-delete-with-laravel-8-n-api-authentication-with-sanctum-19oh
class SchoolTasksController extends Controller {
public function index() {
$query = SchoolTasksResource::collection(SchoolTasks::query()->orderBy('id', 'desc')->paginate(10));
return response()->json($query, 200);
}
public function store(SchoolTasksRequest $request) {
$data = $request->validated();
$log = SchoolTasks::create($data);
return response(new SchoolTasksResource($log), 201);
}
public function show($id) {
return response()->json(SchoolTasks::findOrFail($id), 200);
}
public function destroy($id) {
return response()->json(SchoolTasks::destroy($id), 200);
}
public function update(Request $request,$id)
{
$task=SchoolTasks::find($id);
$task->update($request->all());
return response()->json($task, 200);
}
}