Laravel 9 rest api tutorial | How to make rest API in laravel 9 | Laravel 9 API CRUD from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to start with how to create a rest API in laravel okay so now guys to get started with laravel you need some software so guys for that you just need to install one composer and one xampp server okay so now guys uh let me just show you how you can install the composer just go to this composer and download that composer here is a download and download and double click and install the composer guys so now guys next step was about the xam server so xam server is nothing but the local server which you can work on your system okay so I have already installed that guys this is how xam server looks and here guys make sure you are starting your Apache and MySQL so when you start your Apache and MySQL then you can access the admin I mean PHP my admin you can access your database so if you click on this you can go here on this URL like localhost PHP my admin okay so I have already installed and set up the Zam server guys and I have just created one new database so that is level 9 okay so now guys let us begin with the laravel installation and create the project as simple so now guys let us get back to our folder so when you install the Zam server so when you successfully install the Zam server that time your you will get a Zam folder inside your C drive okay so examp inside that exam I go to HD docs inside hddocs just create any of your folder inside that and you can start working on it okay so inside this font of BYT folder I am going to open a git bash okay or you can use your command prompt as simple guys and now let's get back on the laravel documentation here let's click on documentation and latest version is level nine guys okay so 9x is going on and now we want to create a new project so this is how you can create the project let's right click copy and get back so paste it so here make sure you so now guys I have just given the laravel project name here and hit enter and it starts creating your laravel package and starts its installation so guys once you it starts the installation you can find that folder in your HD docs it creates with the laravel line API folder and inside that you will find all your project code okay so let me just see whether the installation is complete or not so it's still installing guys so guys our project is successfully installed and now guys let us close this terminal and open the project in our vs code editor okay so right click and open it so now guys let us open the terminal here just open with new terminal and now guys let's solve this application so PHP artisan serve so here you find that your server is started on this localhost 8000 so guys now let's get back to our Google Chrome and now let's open a new tab and just type localhost colon 8000. perfect so guys our application is successfully running so now guys let's get started with creating the API so first step guys you need to go to your routes inside your API dot PHP so if you want to access anything around API you need to go to this api.php and here only you will be creating all your API routes and here I am going to create one route colon colon using get method and here and type students and here we are going to just return okay so let me show you a dummy example open and close and function and here I am returning this is student API okay so now what you have to do you have to get back to your browser just type API slash students okay hit enter so you see that this is student API done so we are on this part and now guys we have to manage with the back end okay which means database part so from there we are going to store get everything so we'll be using a postman software to test this API okay so guys now as simple let's get started with the backend part that is your creating a database and other things so as simple so now guys let's open a new terminal here and create the migration first so PHP artisan make colon migration create students table okay so this is a way to create a table in laravel created successfully you can go to your database migration and there you will find at last that is your create students table and here in the up function you have to type your column field names so those are nothing but table of string function so this string is nothing but a very care okay so here I am going to take name course email and phone and now let's migrate this into our database so guys before migrating this into database you need to set up your database okay so for that let us just go to our phpmyadmin and here I have already created the database that is laravel line so I'm going to copy that database name and go to your dot EnV file and inside this dot EnV file we have to move to the DB underscore database so here instead of laravel you have to paste your database name username is root password is null on my local server so guys our EnV is configured database is configured and now let's close this file and now guys let's migrate these tables into our database so for that guys you just have to give one command PHP Artisan migrate hit enter so it's migrating all the table guys so now you can just go back on your localhost just refresh here your level line and here you see all the tables are migrated with the students table perfect so our table is created guys and now let's get back and start with the model and controller part for this API let's write the command to create the model so PHP artisan make colon model where the model name is going to be student hit enter and your student model will be created so you can find that in app model and you find your student model perfect and now let's declare the table and its column Fields so let's type protected dollar table here we are calling the table name so my table name is nothing but students so you can just copy this and paste here and now let us type all the fillable equals to insert array and here you paste all the fillables so guys let's get back on the table field so let me just select here Ctrl d d and just give control forward arrow and then select here let's copy this and paste back in your model in the fillable field and let's add the comma at the end of each data okay now let's begin with the controller part so PHP artisan make colon controller where student controller so I wanted to keep the student control in a separate folder named as API so before your controller name just type API forward slash and then student controller now hit enter so guys you see that controller created let's go to app HTTP where controller you will find the API folder inside the API folder we have a student controller cool so now guys you can start with the coding of creating the API to fetch data let's create a function with the name index function and open and close here I am going to tell a return I am here we miss the function spelling and now guys let us copy this function name and we need to go to our api.php and create our route so create a route colon colon get method and this API name is going to be students then you give comma and inside array you call your student controller so just call the student controller colon colon class at what function you have to call that index function okay so the index function what we have created here so this is the function name we are calling it so on this route you are calling this function and inside that function whatever we get we get as a response so now guys before going there you need to import the class so let's right click and import class so guys if you want this import class option you can just uh install the extension of PHP name spacer and let me remove this we do not require this unwanted data here okay so our class is imported as simple and now you can copy this route and get back to your Google Chrome give forward slash before that you need to add a API prefix okay so if you want to get any API data you just need to add API forward slash then your route names so here if I enter here it tells I am here perfect guys so now let us mingle with the database now you can just go to your student controller so now guys let us fetch the data from our database so let's create one variable called students equal to your student model colon colon all function so this will return all the data and now let's pass this inside a Json format so type response function in the Json so one will be the data your data and then the status that is going to be 200 so before this data I want to send let's copy this here you type the data equal to inside array and you paste that status which is 200 and the message or data you pass it so instead of data let us tell students itself okay so all the students record you will be passing here so let us just copy this and paste here all done guys so either you create in a separate variable here or you can just pass that directly the array format in this okay so it doesn't matter so it's both the same thing and now guys what in case if you don't have a record inside this first so for that let us give a false condition here so if the student let's count how many rows are there if it is greater than 0 then tell yes record is there and that time you can show this response with the student data and Status equal to 200 so now guys in the else condition if we don't have the data then you can respond the message as no record font make this as 404 no record found and here you paste no records found okay a simple so now guys let us just get back and call that API here if we refresh okay we got an error guys that student model is not found you need to import the class so input its class and here I import and now guys let's get back let's refresh perfect so you get that 404 no record found okay so instead of typing student let's tell message okay so it will be more clear m-e-s-s-a-g-e message or status message you can type any custom messages as your wish okay so guys now you have tested this data in the browser right so we have a software called Postman to test all the apis so guys I have already installed this software if you don't have please install it and instead of testing on the Google Chrome Let's test on our Postman okay as simple now you can get back and start with the new request here and we are on the get method so here we have to select get method as we have created the route let me just show you in the api.php so here is my route get method and now we can paste that same URL API slash students and just get the send request okay and here you got the data guys that is no record found okay yep and now guys let's get back and start with inserting the record okay so as simple let's copy the same route and instead of get method let's make it to post method and here you can just use us store function so now you can copy this copy the store function and go to your student controller let me close this table okay go to your student controller and create a simple function okay let me just zoom it guys start with the function public function the function name is store function hit enter let's get the request dollar request and then let's validate all the requests that means input field all the validation we are going to do so as simple let's tell validator equal to Value day star colon colon make function where let us import this class guys first illuminate supports fackets validator we can use it and now here inside this make function you are going to get all the requests request of all functions so it will bring all the input requests add a comma and just give a array and inside that array all the real names so right now I'm going to give name equal to and the validation conditions you have to add required and then use a pipe symbol maximum it should reach to 191 data and same way guys you have to enter for the four Fields so right now if you see we have totally four columns in our database that is name course email and phone okay so according to that we are going to do the validation here so name course email and phone so if your course will be a string you can add the string also here in name and then here you validate the email and then your phone number so for the phone you can do the validation as digits colon 10 and here you can remove it let's use if condition here so validator of f a i l s sales function get the response over here just return response inside the Json format and here you pass the array and give comma and just tell 422 422 means input error okay and now here you type that status was the 422 so errors here you can mention as errors whatever errors we get on the input and get the value validator variable where you will tell messages so this validator is nothing but the variable from here so make sure you are pasting the correct variable and adding the messages only done and now guys once your success is done I mean once the input errors are correctly validated then you will come in a else condition and store the record so guys you can create a variable called student equal to your student model colon colon create function inside this create function you get all your input records and store give array and then you can call all the column field names so this will be my name column field from the database column field name and here your request will come request of name give comma copy and paste and now guys next was our course let's copy this course and paste here this will be the column and this is the input email copy and this will be the input of email and again from the phone copy and paste here the column name phone so now guys once the data is successfully created you get the status from that true or false so if it is true then you will respond something else if it is not inserted then you will tell not inserted okay so for that you can just return a response function in Json format where you will pass the array and here it is going to be 200 which is going to be ok and created and here we declare that status and give 200 success and then one a message m e s s a g e message the message is going to be student added or created successfully done guys and now we can just copy this again and paste here and here you give 500 if something goes wrong inside this and this creating process so you can add here and tell something went wrong and now guys let us just insert the record and see a simple you can copy the route so it's on the same route which is going to your post method let us copy this URL on which the server is started let's copy that and you can get back to the postman and start with the new request here and we have to insert the record so paste it so it's going to be the same URL API and here you are going to change to the post method so now if you try to send what happens let us see that so just send the record on the post method on this URL so here you see guys that is status 422 all the errors you are getting is name field is required course is required email field is required phone is required so you have to give all the input records so for that you just need to go to your body and then you can add through to the form data or else you can add with the raw data also okay so let's start with the raw data open and close calibrous and here when you select on Raw data make sure you are changing as a Json format okay so it is by default it will give text and now you have to change to Json format and now guys the input request so this is my input request name and then you are going to give the value that is um let's give the value as with prakash give comma oops here I missed that it's a Json right so you have to give in a colon and now let's add again the course colon again data here BCA comma again the another email colon phone which is going to be some random digit phone number done and now this will be the fields guys so right now I am going to remove this course I am going to keep it null only and send the record let's see what happens so here you see guys that course field is required he is asking for a data so I have given and one more validation was there let me show you in the controller here the phone number validation is there that is digit 10 is required let us make this as a nine digit here right now it's totally nine and now let's send the request again here and you will find that phone must be 10 digits okay so I will give that 10 digits Here and Now guys if success is there it is going to create the record so let us just send the request perfect so you got the message student created successfully and now guys let us get back to our database and check so inside my students table so here we find the data okay perfect so our data we have got and here also if you refresh you will get the record on the browser that we have got one record and you can go on your Postman also and check that like if you send request and you'll find the one record is available as you have created the record okay and now guys let us just insert two three records so instead of oh wait prakash now let's edit this as ohm BCA and then let's add home prakash and now let's send that here you see student created and now one more record so here I will add one govind Varun gmail.com and then guys you can understand the number again and now let's send the request perfect so you've got again guys student created successfully and now let's see that we have the total number of Records is three so here you see one two and three records perfect so now guys let us display each records so I want to show a record one by one so for that you can get back and start with the code on the API first so guys let us create the route colon colon get method so where I'm going to add this student forward slash the ID okay as simple and now here we'll go to the controller so I am going to my student controller at where show function okay on the show method so now let's copy this show and go to our student controller and create a show function so here I add that public function and the function name is show so we are getting an ID so you need to pass that ID dollar ID and now let's search for the ID guys so a simple create a variable call student equals to your student model colon colon find function where you find this ID so if you don't find you need to write a if else condition when the ID is found that is true then it comes in the if condition if the ID is not formed then you get in the else condition and tell four not four so let us copy from here and paste it and give the condition 404 Ctrl d and here you paste no student font no such student fund okay that is 404 and now guys here once the record is found student respond then you display the data so what we can do we can just copy this response here and paste it so here if the ID is found then give status as 200 and here as student with the data you can copy the same variable and just push that remove and paste so we get a single student so we give it as student only as simple guys and now let us execute this part okay how to show the student record in API so what was the URL structure here let's get to the api.php students slash ID now we can get back and open a new tab guys so here you paste the complete URL let me copy the complete URL what is My URL domain so it starts from localhost 8000 slash API slash then the slug slash then your student slash ID okay so my ID number one I am going to get on the get request a simple let us just check that here it is get only now you send the request you should find the student id1 record perfect yes so here you see only student record of one you have got it which is same as here okay and now let us just get for the ID number three ID3 find code so you get the Varin govind and now guys I don't have the id5 and I am trying to search that ID number so you should get four not four error okay here you get status four not four no such student found and the status is 404 here we are setting from the backend and if you give ID number two and search for that this status is going to change to 200. 200 and it's okay perfect and now guys let's do the condition for edit also so when you try to edit the data okay so you can get back and the same logic goes guys for the edit also just press student slash ID slash edit okay and now you are going to the edit function copy this edit function go to the student controller and paste here I mean create a function so public function the function name edit function open and close calibrase and you are getting the ID let's get that dollar ID and you need to find that data and display the data so it is the same logic of your show method okay so you can copy the same condition and paste here in the edit function as simple guys now let us save this so there is no change it's the same logic and same condition for the edit also and now you just copy the same URL open new tab and here we move with the edit give forward slash and then get slash and edit at the end we are adding this extra slug okay and now if you send this request you see that the same response comes okay so our ID number two is there if I paste ID number 5 which is not available in our record database so it will tell for not for error done and now guys let's update this record so for that let's get back so first go to the api.php create a route to update it so I'm going to use a put method put student slash ID and here you will use the same URL to update it either you write it update but you can keep it edit only and you go to the update function on the student controller so let's move on the student controller and create one function named update so public function function name is update and inside this guys on update we are getting the request all the input requests and also the ID so end of dollar ID and now you can continue with the validation and updating the record so we can copy that code from our store part which is create the student so let's move on the top so here is my store function and this is my validation this is the eiffel's condition and store the record so let's copy that same code and paste in the update function so here we paste now we can start from the top update which is doing the validation here and it fails if the validation doesn't satisfy correctly it fails and then responds with the error messages and if the validation is successful then you come and find and update the record okay so now guys here uh on the so now guys let's change this to update method okay so one step we are going to do let's change this to update and this part we are going to copy and paste on the top and use the find function and instead of here let's remove this and add a RF function okay so you are finding the student and then updating that student record as simple now in the find function you have to get the ID so here we have got the ID from the function and here you paste it so now you need to check if the ID is found or not so you can just control X and paste this below here so here we find if the ID is found then you get inside the if condition and still student updated successfully and if the ad is not found then you will tell the message as 404 okay no search student fund done guys and now let us save and let us test it get back now guys you can just view the rd2 let me just edit the id2 here I have got the response let's take this all the data copy Ctrl C just copy that and paste in a new tab so let's open a new tab and here in the body tab go to your Raw and change this to Json format and paste the data so we have pasted it now guys you need to remove The Unwanted codes I don't need ID let's remove that and here created an updated also not required only this details are required okay and now let's update this to om prakash updated okay and BCA updated so these two columns I am going to update so where are you going to update you are going to update on the URL let's go to the API dot PHP and here is the URL guys so this method you are going to update and it's a method put guys so first select with the method put and the URL is this and here the ID number so my ID number is 2. and now you need to change the I mean you need to add your domain so my domain right now is localhost 8000 slash API paste here and done so you are going on the same route and now let's update it as simple let's send the request and you find perfect so we got that student updated successfully and now guys let us see whether the data is correctly or not so here I am going to just send the request again and display it so here you see on prakash updated and BC updated perfect so we have successfully updated the data also let me try to validate the input here I have removed the course and just send the record on the put method while updating the record so here you see course field is required okay and now guys finally we are going to start with the delete method so let's get back and create a API route so copy and paste guys same put and user delete method and here instead of edit let's just delete itself as simple and now guys you are going to a function name called destroy so you copy this destroy function and go to your student controller let's move there and create one function inside your controller so public function the function name is destroy function open and close calibrates and now guys you can just get the ID we do not require any request so only we are going to get the ID and find the ID guys first so create a variable called student equal to your student model colon colon find function and you pass the ID dollar ID so now guys let us find this if the data is correct means we are going to delete the record else you are going to tell it's not found and yep let me copy and paste here let's copy this for not four and paste it if it is not found then you will respond to that no such student ID formed and here if the student is found then you are going to delete the record as simple now just push that delete function here to delete the record done guys and now let us test this also so yep let's get back and let's open a new tab so here let us call the URL copy it paste here and first you need to change this method to delete guys okay and now you need to give the URL let's copy the URL structure so what is that API structure here let's copy this and paste it so instead of here delete give the ID okay so ID number three I am going to give so what is the ID3 let us see the data here refresh so here you see guys that is Varun govind the ID3 let's delete that and it's just you have to select with the delete method and provide the ID that's it so first I am going to give wrong id id number five which is not available so let's send the request and you should get the message that is no such student found so here we got now if I give the correct ID it's going to delete the record and what message we have set there let me just go and confirm once so here we are not set any messages guys so we missed that instead of missing it let's paste it paste here and make sure it's 200 and tell that student deleted successfully okay and now deleted and then you will get this message yep and now guys let's send the request send it perfect so you got that student deleted successfully and now let us just check in the get method of all the student record let me fetch that perfect so we have got only two records that is id1 and 2 the ID number is 3 deleted so if you want to confirm you can just check with the show also so I want to search for the ID3 which we have already deleted it will tell that no student found okay no such student font perfect so guys we have successfully learned how to create the API arrest or restful API in laravel so now guys you can continue to create your crud application in view react next and many other JavaScript libraries okay using this one single API so guys if you want to learn how to create a crud application in vue.js in react in next you can follow my tutorials the link is given in the description so please make sure you are learning this to create the API and then continue with the crowd applications so guys in this video that's it thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 44,161
Rating: undefined out of 5
Keywords: fundaofwebit, laravel 9 rest api tutorial, how to make rest api in laravel 9, laravel rest api example, how to create api in laravel, laravel 9 api crud tutorial, laravel 9 restful api, how to create crud using api in laravel 9, laravel 9 tutorial, how to make api in laravel, how to create api to insert data in laravel 9, laravel vue js crud, laravel react js crud tutorial, vue js laravel api tutorial, laravel api tutorial
Id: 57Ej3X6tzfw
Channel Id: undefined
Length: 38min 38sec (2318 seconds)
Published: Mon Feb 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.