How to make rest API in LARAVEL 9 | Laravel 9 API Tutorial | Laravel 9 API CRUD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to tech tool india in this video i am going to explain all about laravel 9 rest apis i'm going to explain crud operation with the rest apis i'll show you with the examples you need a basic setup for installing level 9 if you want to know how to install level 9 you can click on the i button and check out the video i have put a link in the description as well so without wasting any time let's get started i have installed a fresh laravel 9 as you can see the current version is reliable 9.6 so in this video i'm going to take a example let's call it a post which will accept two parameter title and the description so to do that let's open this in our code editor so it is a fresh installation i need to configure the database first let's go into the database i have already created a database called laravel api so i just upgrade this database what next we need to create a model which will accept a title and a description for post so let's create it you can create it by running php artisan make model call it as a post and give it a hyphen m to create a migration for this as soon as you create this you'll see this has created a file for migrations if you go to the migrations you'll see this create post post migration here what we need to take we have a field called title give it as a string name it as a title the second field which we are taking is a description for the post i am taking this as an example you can take any other fields which you want alright so we have made our migration ready what next let's migrate it and see if it is migrated successfully by running a command php artisan migrate it will migrate our database yes it's been migrated let's go into our database and check if all the tables are there yes you can see this posts table which includes title and the description all right now let's go to the post open the post model let's close this go to the app models and post dot php here we need to define our fillable property in order to use a mass assignment so i'll use protected dollar field label and define two properties title and description all right so we have our property defined as a fillable here the two fields which we want to mass assign next we want to create a controller for that we need to run a command php syn me controller i'm going to create a folder called api for all the apis and just creating host controller and i can pass a model post so the controller has been created successfully if you go to the http controllers you'll find api folder inside it you'll see a post controller and our post model is already used in this controller all right so we have defined our post model we have created our controller the very next thing is to define and this post controller comes with all the basic red function which is like index function create function store show edit update and it'll destroy so these are the crowd operation which we are going to see today so let's define these roots in our apis let's go go to our roots epi.php for this tutorial i'm not going to use any authentication it's just a simple tutorial without any authentication so i'm going to define this root and i'm going to call it api resource i'm name it as posts and call its controller this should be fine all right so if you want to see what routes we are available so you can just write in a command php addition root list if you see this api roots you'll see all the apis last boost are available here inside epa you see these are the redirected to this api post controller function respective functions so the roots are all set for the apis so the next thing we want to test this route so i'm going to use postman for our http client testing you can use any other http client or you can install extension on vs code you can use that but for now i'm going to use a postman for testing all our http client request so i'm just going to clear this and next let's go to the postman this is the valley url which i have set up for our project if you go to the this you will see laravel epa.test this is a local uh url i have set up on this url if i just write this api slash posts if i hit this url now you'll see you are getting a 200 response but you're not getting any of the data the reason being we are not returning anything here so i just to seek of our preview like it's working or not i'm just going to written a json response here written response json and in that i'm going to pass a status true and posts would be just a blanket let's go ahead and hit our api again you see you will have a status true post is equals to blank so our apis are working the next thing is to create a post and then get the response so i'm going to add a new tab here just copy this post use this so if you go to our route you'll see to create a post the end point is epi slash post but the method is post so it will hit our post dot store function let's make it a post and in a body let's pass a json object here which includes a title this is first post and a description this is first description this first post description i'm taking as example so now we are using our post method to store the post let's go into our function to validate it i'm going to create a request here by just running a command php rtc make request store post request so if you go to the app http requests folder you'll see this store post request i'm just going to authorize it always true and inside a rule what we want to put we want a title title should be a required field so this will be required and the maximum value for this is 70. next thing is a description description would be a required field all right so we have created our request so to store this instead of request i am going to use this store post request as i just use this store post request it includes here on the top you need to use this as app http request and store request then you can use here directly on the store function so what is what this is going to do this is going to validate all the requests coming to this function and if validation fails it will written a validation error to see our validation i just added this store post request in the store function and dumped all the data let's see if this is working or not let's go in our postman so before sending this api we need to add few headers in order to send a json data and get a response in a json so we'll add a header call is accept uh put it as a json and contain type also the contain type of this response request would be json so the request and response should be in the json that's why we i'm sending this accept and contain type as a header in a body i have just put this like only uh title i'm not sending the description if i send this response request you've seen the response the description field is required that means our validation is working properly let's add a description here give it a comma here and add a description test post only so i just added a random description and let's see if it is working now yeah it's working now if you just review this i have dumbed the title i mean whatever we are getting on the request so we are sending the title and description and that's exactly what we are dumping in the controller let's go in our controller as you can see we have just dumped this data let's store this data by post create uh give it as a post is equals to post create and whatever we are getting in the request and in order to return this just written response json and give it a array call it status true and post is equals to post which we have created and here let's pass 200 as its success response and you can add another parameter called message and give it a post created successfully all right let's go to our postman now let's send this you see we got this status as true post created successfully and with all the post data all right so we are getting this response let's go ahead and update our update function so in update also we are going to use store request store post request as a store validation and we are using this post so what we are going to do the just update it whatever the parameter we are getting request of all and just returning the same response as we have written in a create just instead of create let's see update it successfully let's click create a new tab i'm just going to duplicate this tab and this time i have to add a put method here and and pass the id of post if you go our root list you'll see to update this we need to put api slash post slash post host is stands for post id so it will automatically associate this id with the post model so as we know the id is one so i'm just passing this up one and we don't want to change anything in header let's go into the body and see it update it and call it completed all right let's send this request and see what we get in the response as you see we got the post updated successfully and if you see the id is one and we got our title and description updated so this is a working as well let's go to the destroy function as this will just delete the response so you'll see like post pointing to delete and then written the response and instead of passing the post just pass this message and to delete it method should be delete and the urls should be the same let's go and duplicate this tab uh we don't want to pass anything in the body uh method would be delete here let's send this as you see post deleted successfully so we have completed this create update delete let's create an another one we have created this here now let's get update this post here let's take a post function post variable and just pull all the post here and put it here in the variable all right if you now hit the get epi for the post you'll see you have oh let's create another one here and see if we are able to see that in a get response you'll see we have two posts available all right so i think we have covered the basic thread operation of apis it's very simple uh what we have did till now i'll summarize it quickly we have created a post model with the title and description we have created the migration migrated the table and then we have created this post controller with all the basic cred functions and we have used this index function uh in a store function we have created the store post request which will validate our data with the title is required at maximum 70 and the description will required in this and then we have defined our api resource here in the epa.php file and we have taken all the function here in the post controller and in eq store function we have just stored the data and written the response similarly we did on update we just updated the data and returned the response and similarly on the delete we have deleted the data and written the response i hope this will give you the basic information of how to create an api in laravel 9. in the coming videos i will going to explain you how you can authorize the user and written the response in the api i hope this video is informative for you if you found this video informative please do like this video subscribe our channel till the next video keep watching keep learning thank you for watching [Music] you
Info
Channel: TechTool India
Views: 24,237
Rating: undefined out of 5
Keywords: laravel, laravel rest api, laravel api tutorial, laravel api, laravel rest api tutorial, laravel 9 tutorial, laravel 9, laravel 8 tutorial, laravel restful api, laravel api tutorials, rest api laravel, laravel rest api authentication, laravel rest api for beginners, laravel 8 tutorial for beginners, rest api laravel tutorial, tutorial rest api laravel, laravel 8 tutorial rest api, laravel 9 rest api, create a rest api in laravel, laravel tutorial for beginners step by step
Id: VKl9Kd2Moj8
Channel Id: undefined
Length: 21min 4sec (1264 seconds)
Published: Wed Mar 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.