Laravel Rest Api Tutorial for Beginners | Laravel Rest Api Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this tutorial I am going to show you how you can create rest API in laravel so I will start from the very beginning step by step for beginners so first of all let's go and create a facial arrival project so I'll create my normal project over here so let me just open a command prompt here then let me just zoom in and we're going to say here Lara well new let's start projecting will be laravel API tutorial then press enter so this will create a reliable project for us so right now I'll just fast forward the video so here you can see the position is complete so let's put our first directory we will say here CD laravel API tutorial then press enter so let's turn the project PSP artisan serve let's copy this link then we'll go to our browser then paste the link here press enter and these are facilitated larbel project so right now let's just show something using our API in laravel so first of all let's go and open the project using a editor I'm going to use sub-dime text so I'll just click open folder and here my project name was liable API tutorial this one so uh here first of all we'll just actually go to the route here you can see the route folder we click on Route and inside route you will see a pay.php so for writing API we need to write every route in this ebay.php so you will find it inside routes okay so here we are going to say let's say here root double clone okay let me just copy this from the web.php so I'll just copy this Ctrl C and I'll paste this here so here we'll say route get let's say student and stock return view we are going to say return I am from laravel API and indeed with that save clone if we now save this and if we go to our browser then if we just say over here slash student then press enter will get a error so here you can see 404 not found the reason is here whatever we write in the EPA dot bhq it will automatically add a slash API before the route so here our route name is student so before this student in the URL we need to add APN so here before student will say API slash student because it is in the api.php so right now if if we just refresh or press enter here there you can see I am from laravel API we get the message here so right now let's call it using controller so let me just uh go to my project electronic and let me just open another command prompt here and let's just create a controller here so we are going to set PHP artisan make clone controller our controller name will be student controller then we'll press enter it will create a controller for us there is successful so right now we'll go back to our code and here you see we have a controller here which is student controller so first of all uh in the ebay.php we need to restart the controller name so we will say here use app backslash HTTP backslash controllers backslash student controller student controller and after that we'll come down here and we'll say here route get the same thing student then comma to third bracket will say student controller double prone class then we need to give a comma and we need to mention a function name let's say function name will be index give a semicolon so right now we'll go to our student controller and create a function called index so let's go to our app HTTP controller and here you can see student controller so we say here public function in the first bracket to Second bracket and from here we suggest return let's just say something here I am from API controller whatever save this here then here as well then let's go to our browser again make sure to always keep API slash before every URL okay so right now if we refresh there you can see I am from API controller so right now what we are going to do is that we are going to create a table call student and then we are going to call the data using API so first of all let's go to our Command Prompt and let's just create a table called student so let's say here PHP artisan make clone model then our model would be student and we will also create the migration so minus M then press enter successfully so right now let's go to our code again then we'll go to our database migration and insight migration we have student table here so here we are going to set to all our table it will be string type will be sting lesser than first one will be name let's just make it nullable after that we'll copy this and we'll paste it two times one and two so stop name this will be let's say email and this one will be formed save this then we'll go to our model then here in the model we have student.php so here we are going to ADD protected fillable so we will just copy this from the user.php so let me just copy this for user.php and will paste it in the student.php so it will be named email and we have one more field and that is phone number so we'll say here phone save this so right now we can just close the model and the migration and we will just say in the command prompt PSP Artisan migrate then press enter I haven't created the table so would you like to create it I'll say here yes press enter and it created a table and also migrated all the table in the database so right now let me just show you the database tables so let's just refresh here this all so yeah student table and the student table is empty right now so right now let's go and try to get the data from the student table using API so for neet uh we'll just go to our student controller here and stop return I am from API controller will remove this and first of all let's go and add the model student model here so we say here use app backslash models XL student so here we are going to set dollar student equal to we'll mention the model name which is student Tower drone all we are trying to get all data from the student table and after that in API we need to return it Json request so we'll say here return response are not redirect response to first bracket this Arrow sign we are going to say Json to first bracket and here we are going to say dollar data comma 200 200 means a status 200 means success so here you can see dollar Tata now what this will represent so we haven't write anything for dollar data so we need to write here dollar Tata equal to will keep to third bracket like this and we need to indicate with a semicolone after that here we are going to say status uh it will be equal it is greater than 200 give a comma then here we are going to say student now this is just anything you can mention here any name you want but we'll just go with student equal to is greater than and here you can see dollar student so you need to copy this and paste this here and no need to give any comma word here you can write it like this or what you can do is that instead of dollar data here you can just copy this third bracket from here to here and you can just paste this here and you can just remove this you can write it this way as well but I think this is easier to understand for you so that's why I write everything like this if we now save this and if we now call this URL student auto window is that it will get all the student data and it has a response with all the student data so right now let me just show you let's save this here and here so if we now go to browser then if we now refresh eBay slash student here here you can see status 200 and student data is now student table has no data that's why it is showing us this to third bracket only so let's go and just insert a data in the student table so we'll go to our student table and we'll click insert let's just say here name will be yummy email will be yummy at the rate of gmail.com and let's give a random number here then click go so we have a on data in the student table so right now if we refresh here there you can see we are able to get the ID on name yummy email so instead of showing the data in the browser the better way to check the API is using Postman so right now we're going to see the API using Postman so I already opened the postman over here as you can see so right now once you just open the postman you will get the basic view like this okay so here what you need to do is that you need to go to api.php and here you can see our route is gate so you need to set the method to get here you can see get and here you guess the URL option so in this URL we are going to copy this and 1.2 uh paste this here API slash student and right now here you can see the send button if we now click on the same button there you can see it is showing us the data over here student data ID name email and phone number so we are able to see the API data using Postman here so right now I am going to show you how we can store or upload data using API in postpon so right now I am going to show you how you can store or upload data using API so here let me just copy this row and let's go and create it out for store so route for store it will be post let's say the URL name will be same that's fine and here stop index let's say here upload or store whatever so right now we'll go to our student controller.php and here we're going to write the function called public function upload so since this is upload we need to say here request dollar request then we'll give to Second bracket so first of all let's write the validation rules so we'll declare a variable dollar validator let's say equal to we'll say here handing date or tab control and if you want to use validator we need to use uh valuator at the top so we say here use a validator d sum so let's go down and here we'll say May and this view will be Capital so we'll give to first bracket then we are going to say dollar request this Arrow sign will say all to fast rocket then we'll keep comma then we'll give to third bracket like this and indeed with a semic loan obviously so let me just write it like this and here inside this two third bracket let's say we'll set the valuation rule name is required so we'll say name it will be required and let's keep a comma copy this then paste this here and email will also be required you can also add let's say it must be a email number can be null so right now we'll say if we give a if condition if this dollar validator fails so Arrow sign fails then we'll come to this condition and we'll come to this if condition and here Will show a message so we'll say return response it will be a Json request obviously Json to first bracket will say dollar data comma 200 so here for this dollar data we need to write something or sorry if on P 200 let's say it will be four to two so here for this dollar Tata we are going to set all our Tata equal to to third bracket like this semicolone and here we say first status it will be equal four to two and let's say message now what will be the message message will be whatever okay we need to give this Arrow sign here and here so the message will be whatever we have message for the validation rule so we'll copy the validation here validation Arrow sign messages this one and two first bracket basically this so it will return the response with the validation rules of this okay if the validation is failed and if the validation is okay then we'll go to else condition and which total data in the student table then we'll say here let's say dollar student equal to will say new student after that it's just normal insert who is a student error name it will be dollar request Arrow sign name then let me just copy this Ctrl C then Ctrl V so this one will be email we'll say here email and this one will be phone number so we'll say here form so after this we just say dollar student this Arrow sign save and we need to right now return it response so we say return s to first rocket this Arrow sign Json the same thing dollar data comma it will be 200 so right now we just say here dollar data here we are going to say again status it will be 200 and message it wouldn't be I forgot the arrow sign again message it will take Tata uploaded successfully so right now let's just save this so here in the epay.php here you can see the method is post and the URL student that's fine and so whenever we are calling the post function then it will come over here to this upload function it will check the validation if the validation fail it will return a error message response and if the upload is successful it will return us the message start our product successfully so right now we'll go to our Postman and here let's just open new tab then here you can see on the left side gate we'll choose the post option and here from here I'll just copy paste and paste the same thing over here API slash student and right now if I click this send button okay it written us with some error I guess so let me just copy this URL Ctrl C and I'll go to my browser you can just paste this here press enter okay syntax error return okay I forget to give semic loan after the data so let's go back to our code and here is the same clone and here I miss the semicolon save this again let's go to our Postman and let's just click send button again not found in file in line 33. let me check it line 33 valid or equal to validator make request to all it's fine totally fine here okay let me check okay this V will be capital or smaller validate dollar so let's say this so right now let's go to postman again and let's click on the send button and there you can see it is showing us a message that the name field is required the email field is required right now what we are going to do is that we are going to upload the data in the student table so for it here you can see the body option which click on body option then we'll click on this raw option raw and here also click raw it will show you this option text so we are going to choose Json and right now we are going to give to Second bracket here then we are going to say here let's say only name let's say name clone here you need to give clone and let's just mentioned name will be John toy in John doy and here in the validation rule we write name and email both is required so right now if we just open our Postman and without giving the email if we click on the send button it will set email field lists also required so here you must keep a comma about here then here we're going to say email clone to Tower quotation let's just say email in p John at the top gmail.com let's give another comma then let's also just keep the phone number as well phone it will be let's just say random number and then here we're going to click on the send button and there you can see data uploaded successfully which message came from here you can see in the else condition we write message equal to data uploaded successfully so right now if we just go to the gate method uh here you can see so right now in the get method if you click on the send button there you can see there is one more data here you can see John Doyle John at that of gmail.com the phone number and create an updated date so right now let's go and check it in our database so here uh let's just click on the student table again and here you can see in the student table we have onboard data here so right now let's go and just add onload data using Postman uh so here instead of Chong we are going to say here Adam it wouldn't be interested Adam whatever number whatever it is that's fine and we'll click Send again and that upload is successfully let's just refresh out get request and there you can see we have three data right now so right now I am going to show you how you can edit or update data in using API so for it let's go and first get the route here so we'll go to our ep.php and here let me just copy this paste here and we'll say stop post we'll say here for init we need to write put and let's say student slash edit slash to Second bracket we we need to mention ID here and let's say it will be edit function name save this and right now we'll go to our student controller and write a function for edit let's go to student controller we are going to say here public function edit we need to say here request then dollar request then we also must need to match on a ID then to Second bracket so first of all we can apply the validation rule here as well or you can skip it okay let's just do the validation route we are just going to copy it from the approved function so we'll just copy everything uh from here from this validation to up until uh the insert okay from here to here so Ctrl C and we'll paste it in the edit function Ctrl V so validation rule will still be the same but over here you can see student equal to new student so we cannot say this so we want to update specific student so we need to say here dollar student equal to we need to mention the table model name which is student double clone we need to say here find dollar ID this all so it will find a specific ID and then update the data and then here you can see data uploaded successfully it will be updated let's say here updated successfully so let's set this and here you can see for the URL for edit you must keep put and here you can see this the URL for edit so let's go to postman again let's open another tab and here we're going to choose pool and we can just copy this from here the link paste this here and we're going to say here slash edit and again we need to give slash and we need to give a ID here so here how many ID do you have we have on 2 and 3 ID total so here we need to give ID let's say ID on then press send option here and here you can see you will see the message the name field is equal and the image fing is required why because uh in the edit function here you can see first of all we are checking the validation name and email is equal and since we do not give any name or email over here so that's why it will return us this message so right now we'll click on body and then Rob option this option again text and Json so here we want to update the ID on user so you can see ID on here so what we'll do is that here you can see ID on equal to Yummy email list this one and phone number is this one so let me just copy this from here and we'll go to our Port request here here you can see put so first of all we'll give to Second bracket inside this we'll just paste it and here uh let's just say stop Emi will say here Yamin Shakil and email will be Yamin gmail.com we will only update those two data and right now if we click Send option here you can see data updated successfully so right now let's go to get request again and here you can see name and email is this one so right now if you now click Send it means if window now refresh our base here you can see for ID on name and email is updated okay so this is how you can update data using API in laravel so right now I am going to show you how we can delete data using API so this is super simple actually for delete data so let's go and first write the route so let me just copy the route for Port then paste it here and stop port in order to delete we need to mention delete let's say we will use the same URL that's fine and stop edit we are going to say delete save this let's go to our student controller and here we are going to say public function and delete to first bracket and first of all we need to get the ID obviously if you want to delete something and here we are going to set dollar student equal to and so the student model name Tower clone find dollar ID then we are going to say here dollar student this Arrow sign delete as simple as that so right now we need to return in Json response so we say here return response to first bracket this Arrow sign Json just like before rule set all our data comma 200 and let me just say here color data equal to tooth Hardware arcades individ with the semic loan I always forget the semic loan for some reason I don't know why here we are going to say here status equal to it will be 200 then we will say here message it doesn't have to message every time you can say any variable if you want so message will be data deleted successfully save this so right now what you need to do is that we only just need to write this for the delete right this URL for delete so let's go to postman let's open another tab and right now we'll click on this delete option and here you can see that route will be student slash it is slash ID only this one the same export but we need to mention the delete over here so here we can just copy it from the put option from here let's say uh we will delete the ID number uh let's me choose this on ID number three Adam Doyle this one so here in the URL you stop on we are going to say here 3 then click there is another error on line 146 let me check 146 status okay I forgot to give the arrow sign here and here set this then let's just click Send again and there you go data deleted successfully so if you go to the get request again and if we now refresh I mean click on send button there you can see there is only two ID right now IDT is deleted so this is how you can create API in laravel I hope this tutorial is useful to you if you have any question you can let me in the comment section and if you want to learn more about laurable then you can see the video on the skin you can check them out if you want thank you very much for watching make sure to subscribe I really appreciate that and I hope to see in the future tutorials
Info
Channel: Web Tech Knowledge
Views: 12,438
Rating: undefined out of 5
Keywords: laravel rest api, laravel api example, laravel api test, laravel api, api in laravel, create api in laravel, postman, postmanapi, api with laravel, laravel rest api example, laravel rest api tutorial, laravel api tutorial, laravel rest api for beginners, postman api, laravel api tutorial for beginners, laravel-api example github, laravel rest-api github, laravel 10 api tutorial, How to Create REST API Using Laravel, how to make api in laravel, how to build rest api in laraavel
Id: Vik6oo4O7xo
Channel Id: undefined
Length: 29min 6sec (1746 seconds)
Published: Thu Aug 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.