.
Run Command
php artisan install:api // it will create api file in routes folder
Now make model with migration and controller
php artisan make:model Post -mc --api
now in migration folder
$table→string('title');
$table→string('body');
and in model
$protected fillable=[
‘title’,
‘body’
];
Now in controller
$post=POST::get();
return response()→json([
‘message’=>'List of Pages',
‘posts’=>$post
],200);
now
php artisan migrate
Route::get('v1/post',[PostController::class,'index']);
Now if i want to store post
public function store(Request $request)
{
$post=new POST;
$post→title=$request→title;
$post→content=$request→content;
$post→save();
}
return response()→json([
‘message’=>'New Post Created',
‘posts’=>$post
],200);
When you are using put you need to put method in form data in your request from postman
now with validation
We can do validation by inside the controller or making request
First by making request
php artisan make:request StorePostRequest
now inside app→http→request→your request file
First of all make authorize true in authorize function return true, then we can use it
now inside the rules function write your validation
return [
‘title’=>required | string,
‘content’=>required|string
]
now in your controller
use App\Http\Requests\StorePostRequest;
public function store (StorePostRequest $request)
{
}
in header insert accept key becuase you told server we need response in JSOn not in HTML then our validations will start working
now mention fillable field in model then in controller you can also update data like below you don't need to put extra things in your request
public function store (StorePostRequest $request)
{
//if we want to insert data
$post=POST::create($request-validated());
$post->updated($request->validated());
}
Now we can refactor our response because we are using the same code to send again and again and its actually cause code duplication
Note we are going to use abstract class and we can not make object of abstract class
now make a controller
php artisan make:controller ApiResponseController
now in your main controller like POSSTController
class PostController extends ApiController
Now in your api controller
public function successResponse($data, $message='success',$status=200) // These values are default if function is not get values then these default values will be used
{
return response()→json([
‘message’=>$message,
‘data’=>$data
],$status);
}
now in your main post controller
return $this→SuccessResponse($post);
now for error in controller class we define another function
public function errorResponse($data, $message='Error',$status=404) // These values are default if function is not get values then these default values will be used
{
return response()→json([
‘message’=>$message,
‘data’=>$data
],$status);
}
now in main controller pot controller
if(!$post)
{
return $this→errorResponse('Post Not Found');
}
else
{
return $this→SuccessResponse($post);
}
we can add prefix also like below
and if we want to get rid of this chunky code we can use api resource while we are making controller
make controller like below
php artisan make:model POSt -mc --api
you can check route list in command line
php artisan route:list