---
title: 'Laravel 11 rest api tutorial | How to make rest API in Laravel 11 | Laravel 11 API CRUD from Scratch'
source: 'https://youtube.com/watch?v=WumgBzENYYk'
video_id: 'WumgBzENYYk'
date: 2026-06-15
duration_sec: 0
---

# Laravel 11 rest api tutorial | How to make rest API in Laravel 11 | Laravel 11 API CRUD from Scratch

> Source: [Laravel 11 rest api tutorial | How to make rest API in Laravel 11 | Laravel 11 API CRUD from Scratch](https://youtube.com/watch?v=WumgBzENYYk)

## Summary

This tutorial demonstrates how to build a complete REST API in Laravel 11, covering CRUD operations for a products resource. It walks through installing the API scaffolding, creating migrations, models, controllers, and resources, and testing endpoints with Postman.

### Key Points

- **Introduction and Setup** [0:00] — The video starts with a fresh Laravel 11 installation and serves the application at localhost:8000.
- **Install API Scaffolding** [1:02] — Run `php artisan install:api` to install API scaffolding, which creates api.php routes, personal access token migration, and the HasApiTokens trait.
- **Add HasApiTokens Trait** [1:45] — Add the `HasApiTokens` trait to the User model to enable API token authentication.
- **Run Migrations** [2:44] — Run `php artisan migrate` to create the personal_access_tokens table in the database.
- **Create Products Migration** [3:06] — Create a migration for the products table with fields: name (string), price (integer), description (mediumText).
- **Create Product Model** [4:13] — Create the Product model and set the table name and fillable fields (name, description, price).
- **Create API Controller** [5:09] — Create a controller at `app/Http/Controllers/Api/ProductController.php` using `php artisan make:controller Api/ProductController`.
- **Define API Routes** [5:54] — Use `Route::apiResource('products', ProductController::class)` in api.php to generate RESTful routes.
- **List Routes** [6:25] — Run `php artisan route:list` to view all registered routes, including the API routes for products.
- **Implement Index Method** [7:16] — In the index method, fetch all products using `Product::get()` and return them as a JSON collection using a ProductResource.
- **Create Product Resource** [8:24] — Create a resource class with `php artisan make:resource ProductResource` to format JSON responses.
- **Test Index API with Postman** [10:44] — Test the GET /api/products endpoint in Postman. Initially returns empty array, then after adding count check returns 'No record available'.
- **Implement Store Method with Validation** [12:32] — In the store method, validate input using Validator facade. If validation fails, return 422 with error messages. On success, create product and return success response.
- **Test Store API with Postman** [15:44] — Test POST /api/products with JSON body. Initially fails due to validation, then after fixing validator, successfully creates a product.
- **Implement Show Method** [19:30] — In the show method, use route model binding to fetch a single product by ID and return it using ProductResource.
- **Test Show API with Postman** [20:37] — Test GET /api/products/{id} with ID 1, returns the product data. Also adds more products (mango, grapes) for testing.
- **Implement Update Method** [22:04] — In the update method, validate input, update the product using `$product->update($request->all())`, and return success response with updated data.
- **Test Update API with Postman** [24:08] — Test PUT /api/products/{id} with ID 3, updates grape name and price successfully.
- **Implement Destroy Method** [25:35] — In the destroy method, delete the product using `$product->delete()` and return success message.
- **Test Destroy API with Postman** [26:45] — Test DELETE /api/products/{id} with ID 3, deletes the product. Then trying to fetch ID 3 returns 404 error.
- **Handle 404 Errors Gracefully** [27:46] — Modify `bootstrap/app.php` to render a JSON response for ModelNotFoundException, returning 'Record not found' instead of HTML error.
- **Final Verification** [28:37] — After handling 404, fetch all products again to confirm deletion. Only ID 1 and 2 remain.

### Conclusion

The tutorial successfully demonstrates building a complete RESTful API in Laravel 11 with CRUD operations using API resource routes, validation, and JSON resources. The next video will cover authentication to secure the API routes.

## Transcript

hey guys welcome back so guys in this
video we are going to learn the complete
rest API in larl 11 version so guys I
have already installed a fresh laral 11
application here and and also served the
application at Local Host 8000 which is
running over here okay so now guys let's
get started so let's open our project in
our editor so I use vs code editor so
first step uh after installing your
larel project
let's install the API also so let's open
the terminal and click on new terminal
so before installing I just want to show
you few things like here routes so in
this routes you see you don't find
api.php file and inside your database
you don't find few migration about the
API personal access token and also guys
about the model so in the model user.php
you don't have a trade called has API
token okay so guys now let me
just get started with installation PHP
Artis install colon API hit enter so
your installation
starts so guys our installation is
completed and here it's asking a
question like new database migration has
been published would you like to run all
the pending migration files yes or no so
right now we'll give no guys because we
want to see our database connected or
not and here you see API scaffolding
installed please add the has API token
trade to your user model okay so make
sure you're copying this now let's
minimize the terminal and first let us
go to your app model user.php inside the
user model we have to add this has API
token
so let us give as use now you can copy
this at the last of your use inside the
class user model you have to add this
has API token perfect so now guys uh we
have added the has API token and now
let's move in the migration so database
migration you find the new table about
personal access token okay and also
you'll see a new file inside the routes
api.php
cool so now guys let's migrate that so
before migrating please check do you
have connected your database or not so
yes I have connected here using MySQL
and larel 11 database so now guys let's
hit the command PHP rtis migrate hit
enter and your table is
migrated now let us get back and see do
we have a personal access token table
here so let's refresh so guys here you
see we have a personal access token
table okay so now so first step guys we
are going to start with the API CED
operation with an example of products so
let's create a migration file named
products the HP Artis make colon
migration
create products table hit enter and here
migration is created now you can go to
database migration and the last and now
let's give the fields so we'll give a
string the product
name then the product price and one more
field let's add that as a medium
text text function which will be
description okay and now let's push this
table into our database so PHP Artisan
my
hit enter it's migrated now you can find
that products table Let me refresh the
table here okay products table cool so
we have migrated it now we can get back
and let's create the model so PHP
artison makeen model the model name will
be product hit enter so our model also
is created product.php let me close
this and go to app model your
product.php
perfect let me just zoom it so first
step guys we will declare the table like
product table equal to your table name
products is the table name and then
protected dollar
fillable will be all the fields so what
are the fields you can just copy from
your migration
file that is named description and price
let's close
name
description
price that's it and now let's create One
controller so PHP Artis make colon
controller and I want to create this
controller inside a API folder API SL
product controller hit enter and your
controller is created so you can find
that in app HTTP controller uh API
folder inside that API folder you find
your product controller cool so now guys
let's begin with creating the functions
like public function the function name
index function and here is a typo so in
this index function we will get all the
records of the product so before getting
the records we have to create the API so
let us go to our routes api.php and here
let's let begin so first step guys we
are going to create a route using API
resource route okay so now guys let us
give
the URL so creating a route products and
let's mention the
product controller colon colon class so
make sure you're importing the class
guys which is already imported
automatically and now guys we can uh
check the route list for this product so
let me open the terminal PHP Artisan
route colon list hit enter so let's move
out the top and here you see the API
part okay so this will be our complete
API route so now guys we can just follow
this and create the complete
functions so let's move inside the
product
controller copy
this create for the store
and for the
show and
update and then
destroy that's it so now guys let's
begin with the index function so first
let's create a variable called
products equal to your product model
colon colon get function okay so getting
all the product data
and import the product class so it's
imported here guys and now let us return
this in jsn format so first I will check
if there is
records like
yes
else so if a record is available then
show else you can
return response function in JSO n
format and reply 200
here and give the message
as um no
record available okay so if the record
is found then we are going to return the
Complete product data so how are you
going to return guys we will be using
the resource part so let me just a new
terminal and create a new resource PHP
Artis
makeon resource
the product resource okay hit enter and
your resource will be created so you can
find that in the app HTTP directly
resources folder will be created and
here is your product resource so guys
this resources is used to respond in
Json format okay so here you see Json
resources it uses so guys now you can
use this
product resource so you are sending
multiple data so you have to use in
different way like just return product
resources colon colon
collection function and inside this you
paste this product variable directly
okay so whatever data you have in this
you can access each column so before
that let me import this class okay we
have imported and now guys all the
column Fields like name description and
price at this product you can access in
this product resources so right now you
see here that it's returned directly
parent to array so instead of this you
can return in this format
also like the
name of dollar this of name okay so same
way guys you have to change for the
description
description then price if if you want to
take the created at column so you can
take it same way created
at
Price description and if you want to
give name or else
product name so anyhow it depends on you
so this will be the response output okay
okay so let me just keep it name itself
and we can get the ID
also done so guys the difference is here
you can can manage only like what Fields
you want to show or else if you want to
show complete thing you can just keep
the same okay so guys I will prefer
using like this so now let's get back
and yep so guys let us test this API
like this index function part so let's
go to our api.php and to test this API
I'm using a postman software so let me
just open so guys this is a postman
software which I have installed so now
guys let's make make a new request here
just click on this plus icon and here
your request sends so now guys let us
copy the API get back and by default you
have to give API and then products so
what is the complete URL guys let us go
to our browser just copy this Local Host
8,000 and paste
here let me remove this extra forward
slash and let's send the request so if
you send so let's see what happens now
so here we get at the error guys like
raw so internal server error which is
telling the collect function does not
exist so it's not collect it's
collection guys so let's go back to
our index function and tell
collection and now get back send the
request and here you see by default data
has come okay now you can click on this
pretty so here you see in the object
data you are getting this empty array so
guys here it's not able to check like it
has data or not so you can use just
count function if it is greater than
zero then you have to return this else
return this so now get back again send
the request so here you see the message
no record available and the status is
200 so you can see at this okay so now
guys uh let us get back and and start
with inserting the record using the API
so it is very simple guys just use a
simple request here get the request
dollar request and do the validation
first request of
validate function and use the name field
okay this will be your input field value
and what will be the validation it is
required and it's going to be a string
value and then Max
255 same way for
the price will be integer and you can
remove this
Max so this will be the price and for
the
description it is required string and
Max should not be there okay so now guys
you can get started with inserting the
record into your model I mean product
table so let's use product model colon
colon create function in Array format
and you get all this each Fields so let
me just copy and paste here and insert
this just get the
request
of name okay so this will be a name
description so you have to just paste
that let's paste here paste it so my
input request description and input
price so once product is created you
have to send let's response so return
response function in JS n
format in
Array 200 and here array will be like
message
of product
created successfully and also you can
respond the data guys which
is the
dollar product equal to Dollar product
okay and here you set the
data instead of sending the complete
model we want to send our format data so
which is the product resource so let's
get back on this let's copy the product
resource
and make as new product resource
function okay so now guys let's get back
and create a product record so let me
just copy this complete API and paste
here so guys to create or insert the
record you have to use the same route
which is shown here let me just show you
in
the route list so here you see guys that
is about the post request which is
product. store it goes to the store
function and this is the API so using
post method and the API stat so now guys
let's get back to our Postman
and yep so this will be the API and it
is a post request so let us select that
post method and now guys let's go on the
body Tab and here you have to select
with form data or raw data okay so now
guys let's add the fields so make sure
this raw data which you are entering it
should be in jsn format so select the
jsn format here first and then you can
work on it so now guys let's add the
fields like name colon and here I'll
give null and the field
description description colon null and
finally the field price colon
null okay so now guys let us uh just
click to send the request what happens
now if you click
Send uh here you get an error like let
me just click on preview so here you see
guys that is it is showing the main page
homepage so I think it's not able to
handle the validation part so let us get
back and move at the store function at
validate code so instead of this
validation we'll be using a validator
method so let's create a variable called
validator equal to your
validator class colon colon make
function and inside this function you
have to pass dollar request of all
function and then use array and give all
the input Fields so here you just copy
and paste the
fields and yep so now you can remove
this now let's copy this
validator
if
validator fails
function then just return response
function in JSO n
format and the error will be
422 so in this uh you can send the
validator messages so let me copy this
variable and mention that error then
Arrow function dollar validator of me S
AES messages function okay and if you
want to mention like the
message
uh all
Fields fields are
mandatory okay and now guys uh import
this valid data class so class import so
here you select with eliminate support
fets valid data let's click on it and
yep so you can just
move and update that valid dator okay so
here our valid dator is imported perfect
so now guys let's get back to our
Postman and send the request again so
here all the fields are empty right now
let's click to send the request so here
you see all fields are mandatory the
message is and the error inside that it
is showing name field is required
description required price is required
so let us add the product like
apple and this is Apple
description and I will keep the price
empty let's send a request so you see
again that it is asking for the price
field and now if I give
like 250 and let's click to send the
request so here you see the product
created successfully and this is the
data guys okay which is just shown only
whatever Fields you want so now guys let
me just show you in the database whether
it is stored or not perfect so here you
see guys our record is stored and now
guys let's get back to the editor and so
we have done with the create product and
now let's move on to view the product I
mean fetch each product so for that
let's get back to the api.php and this
is the API resources guys and according
to this to get the data one data you
have to use this route so which is
coming via show method so here is the
show method with the product product ID
and using a get method okay so what you
have to do you have to use this product
product variable inside your controller
so let's copy this product and get back
inside your show function and here
provide the model name product and then
the product variable so make sure this
variable is same as provided in your
route list so this is the variable guys
product so I'm pasting that product so
now let's return this so return inside a
gson format you can return or else with
the help of your resource that is your
product resource you can just use this
and return it okay so it's the same
logic guys returning with the status 200
with the your field data okay product
datas so it's the same so now guys let's
save and yep let's go to the postman
let's click on new tab for the new
request and copy the API so let me just
copy this get back and paste Here and
Now guys the route is like providing the
ID so ID number one here okay so now
guys let's click to send the request so
here here you see you have got the data
that is id1 and the details so let me
just add few more products okay so here
I will add like M mango this
is mango
description and the price is like 150
and let's send the request to save it so
here product created successfully with
the mango details and let's with the
gpes grapes G PES grapes and this is
going to be like 100 let's send the
request so here we are so product
created and now let's get back to the
show detail okay so let's send the
request here once again and with the ID
one so let me get the Grim item so
product let's send and here you see for
the ID number three we have got the
grapes perfect so this is how we work to
get the data okay so now let's get back
and continue with the update part so how
do you update this details okay so you
want to update this graes record so for
that uh let us get back to the code and
write the code for update the product so
for that guys according to this API you
are using uput or patch method and here
is the variable that is product which
goes to the update function so let us
get the product variable I mean product
model and dollar product variable okay
so this is the same variable you are
getting it and also we need the request
like we are sending the input request
from this Postman right so to get that
please add the request also request of
dollar
request comma
done and then guys let us add the
validation and the update code and then
return
the data so let us move at the top at
the store function so here we have
already written the code for the
validation and then create and then the
response so let us copy this complete
code and paste in the update function so
now uh let me just arrange it here it's
gone
messy okay so guys First Step uh doing
the validation let me close this
terminal so we are doing the validation
and responding that all fields are
required if validation is done then you
will start with the update part so here
I'm just going to remove this complete
model and just use update function
that's it okay so this is the variable
guys from the update function you are
getting the product variable so using
that product variable you are updating
that particular record and this will be
the
fields cool so now guys once once
updated you will return that product
updated successfully and return the
updated record okay that's it so now
guys let us get back to the
postman and yep let's open a new
request and let me copy the
API so here we paste and what method you
have to use you have to use the put
method so let me just show you put or
patch any of this method you have to
use and let's use it put method itself
so you are using the put method and now
get to the body part and at this body
you have to select with the form data or
raw data anyhow so if you are selecting
a raw data please select this in Json
format and provide the fields so let me
just copy from the post request guys so
we have already typed it let me just
copy this and paste here okay so on this
ID number three I want to update this
grapes to g r a p e grape okay and here
also grape and this is grape only we'll
keep let's send the request so let's see
what happens send in the request and
here you see product updated
successfully and the grape spelling is
changed okay so let me just show you
again with the pricing like I will keep
50 send the request and here you you see
the data is price 50 so guys we have
done with the updation part also cool so
now guys let's begin with the last
option that is destroy which means
deleting the record using the API so let
us get back to our code and this is our
public function destroy function and in
this destroy function how you can use
please check out your route so here you
see it is using a method delete and the
API is same like as we edit upd dat so
same way you can delete Also let's call
the product model P doct product model
dollar
product so from where are you copying
this variable product so here is the
product variable guys just copy this and
paste here and it going to the destroy
function so now let's use this
product and use delete
method that's it so this will delete the
record and then you have to return a
message so let me just copy from above
and paste here and here we don't need to
show the data because we have deleted it
so let's mention the message as deleted
successfully okay so now guys uh let us
get back to the postman and let's copy
this
API get to the new request paste it and
please select the delete method so using
this delete method and sending the
request with the ID3 I want to delete
the ID3 data and now let's click to send
so once you click to send it will delete
the record so let's click on send so
here you see product deleted
successfully and if you try to get the
record with the ID3 you'll not be able
to get because it's already deleted so
let us see that let me just move to the
third tab here which is using a get
request and this is the ID3 let's click
to send so here you see we have got the
response guys and let me just preview
here you see that 4 not4 perfect so here
you see that 4 not4 means it's not found
so ID3 is not found but guys this is not
the correct way to show the output
because it is a API request so for that
guys you have to just add a simple code
inside your uh go to bootstrap inside
this bootstrap app.php and inside this
app.php with exceptions so let's go to
the larel documentation on error
handling so here I found that how we can
skip this so just copy this code guys
and paste inside this exception part
okay and also import this not found HTTP
exception and also the request classes
so here it is let us just copy this
contrl C and import or paste at the top
that's it now done so guys let us close
this app.php and get back to your
Postman and send the request again let's
click to send so here you see message
record not font cool so guys let us just
get to the first uh request that is
fetching all the products so let's fetch
so here you see that on this request you
have got all the products so ID one and
id2 that is Apple and mango product
third product is deleted
okay so guys we have successfully
completed with the restful API crowd
operations using the API resource route
okay so guys in next video we'll be
seeing about how to create
authentication for this route and secure
this route so guys in this video that's
it thank you for watching this video
please subscribe like and share
