RESTful API CRUD with Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on guys hope that one will be fine and welcome back to another tutorial so in this video i'm gonna explain you about the restful api in laravel we will create api using laravel all right so if you guys are not sure what is basically restful api so a restful api is an application program interface that uses http request to get put or post or delete we have different ways to get the http request so in our case you see that we have our uh we have our server running on the port 8000 with the 127 point which is the localhost right so you know i'm using postman here you can install the postman by using go you have to visit the postman or let's go to the postman and you can install the postman.com and you can download from here all right and now you can see that i have my server running and i already added up my http 127.0.0.1 and which is a get request and i i once i send the request this is going to preview me the same thing which i have running on my browser because we have running the server right so this is basically the the meaning of the of sending the request which is the get request because we are getting the data or getting the server running on the on our port 8000 so we can also explain restful api as a representational state transfer our restful api is also referred to as a restful web service you can use as a service and for example if you create an api and you build an api and you want to uh get results using the other uh client side like if i want to get this api using uh another angular um and and and and use it as a preview or use it as a request to form an api using services which are later gonna explain you everything so uh it's it's basically the client and the server are independent of each other of each other so what actually it means that if i have this if i have any if i have in a route and which includes the all the customers information so what i'm going to do is to get this get this request and use it as a server a service in the other application you can use it in angular you can use in in your other like view or any other um any other framework that that we're there that's where you can use as a service right so that's actually it's all the meaning so i think so rather making it complex for you let's get started and we can explain you everything on the code right so i have already installed my laravel application right so you can easily simply install that and i have a lot of videos where i've installed the laravel and even in my pre in even my in my all including series for the larva right so let's create our v first api and the first thing you need to do is to get your dot env uh you must have your database inside your laravel underscore api and i have already created the database so let's get into the uh laravel and let's go into the laravel underscore api and i need to delete that table which i already created already even even though i need to delete all every table right so let's gonna drop everything which i have already created before but i want to create everything new for you so you have to create your uh name of the database inside your dot env and next thing you need to do is to go inside your um you have to create your tables but before creating tables and migrating it you need to first configure add the uh provider's app provider app service provider so let's use the laravel here so use illuminate sport facades schema right so let's add the schema inside the boot function so schema default string length and give it the size of 191 right okay so let's create the model first so let's create a table uh for creating the table i'm using the terminal let's close that up and uh you first you need to do is to create the model let's create a model php artisan make model the name of the model in my case should be employ and dash m for migration right and let's press enter but before pressing and press enter this is going to create the employees table so all i'm going to do is to go inside my database migrations and that's going to create a migration for us so which should be create employees table all right and let's create here the uh table of type string and define here name and let's do the same for the email which is of type string uh it should be email right all right so let's create the uh integer type for the uh salary let's add the salary all right so we added the three main uh columns for the table of employees so let's migrate it right so php artisan migrate right so i migrated and that's going to create the table so let's go see on a database so you see now we have our employees table including with the users which is by default created right now next thing i'm going to do is just close that up and let's create a controller with the name of so php artisan make controller and that should be employ controller all right so i created the employee controller let's get into the employee controller and let's start with our very first api right so the first thing i'm going to do because we are writing an api there are two ways that you can define in your web.php the better way is the adding the api.php so let's get into the routes and api.php file and let's gonna write our very first api for the get request so i'm using here get request and the name of our route which should be employees and the name of the controller which is employ controller and let's give it the name of the method is get employees all right so get employ and save it and let's go into your controller app http controllers employee control controller so let's create a function with the name of get employee and let's put up here so let's put up here the request but before doing it you need to use the model so use app slash employ which should be e should be capital all right so use app slash employ and i'm using here aloquin right so here for for doing this very simple you just need to return response which should be json response and i'm going to bring up here the employee column all for getting all the uh all the columns for we are using all for everything which is inside the employee table right and uh because we are using here uh status code which is 200 200 means that are successful right everything is okay right so let's get i i have already explained i'm using postman uh for testing my api you can install it now in order to get this request i'm i have to use here the get request and slash api and it should be because we have our routes inside our api.php so you have to use slash api and next thing you need to do here is to add the employee so let's copy that up and add here employees go into the raw or you can use any pretty i believe that's going to be and the request which is get let's send it and i believe this should be an empty array but before doing it it's saying your error connection refuse so because we haven't run our server so let's run our server with the php artisan serve right i run my server and now you need to go to your database and let's add some data in order to uh to test our api um so i'm gonna add some data inside the employees but in case if you want to still send the request this is going to show you empty array right so now our api is working and you can see everything is working fine for you right the status code which is 200 okay right now let's add the data and check this out so i'm going to go inside my employees table and let's add up here let's add up here john add the rate gmail this should be the name so john there at gmail dot com and let's add the salary of 500 and let's add another one all right so let's add the salary of 600 all right and i believe let's go right okay now i have my data inserted now in order to test our api you need to click on the send and you can see now we have our id1 have it's on data and id to have its own data all right so i hope you guys can get an idea of how to use the get request all right now let's get and add another api where we can where we can see the detail uh information only about the specific user which have the id of one in order to do that you only need to do is to first let's give it the let's give it the get all employees all right so let's write another one here and let's get a specific employee all right so let's write the route for it so route get and because we are using here the we are we are finding the specific user so i have to define here employ and slash and i need to add the id because i need to check individual user information so i have to grab the id of the specific um employer right so let's give it the name of the controller which is the employee controller and let's add the method with the name of get employed by id right so i'm gonna copy this id and let's bring up here so public function get employed by id and this id is gonna pass inside my controller all right so let's grab here dollar id and in order to find the id inside the eloquent all you need to do is to use a fine method so employ find and find it with the id and uh and i'm just adding up a check here and i just need to check if it's null so i'm using a method which is is underscored null and if that employee is empty right then i need to give it a response return response of in json so let's add up here a message so i'm using here the keys and the values so let's give it the message first so i'm going to give it the message with the value of employee not found all right so employee not found and i give it the response of four or four means that there is 404 means the status that not found right and in case if we find it so let's return response of in json and set that to be employ and give it the employee fine dollar id and give it the request of 200 all right so let's get and start and test this out so the name of my route which is employ slash with the id so what i'm going to do here is remove this and let's add the id which doesn't exist which is three because in my database you see we have only id1 and id2 so let's first test with the um [Music] with the id of uh three you see that employee not found and in case if i add up here slash two and press enter and you see now we can only grab the result that we needed so we have information about the id2 now in case if i need to review the id 1 and i can easily grab the id right so we can get the detail of the user now now let's add up a new user and in order to add the new user so let's create an uh route for it so so add new and add employee so let's because we are adding a new employee so i'm using here post request so remember anything which where you have to submitting the form you have to use the post request so let's add here add employee and the name of the method should be employ the controller should be employee controller and let's give it the name of the method name which should be add employee go inside the controller so public function add employee now because we are adding the employee i need to add the request so let's add a pair request all right and adding the requests employ we need to create a new object so employ um call and call and create let's create the all requests so dollar request dash all right and at the end i need to give it the response so let's give the response of um 200 or you can also give it the response of 200 which 201 so i just need to confirm 201 response code so the 200 created success status response code indicates that the request has succeeded and has led to the creation of a resource right so 201 response i need to add up here so let's add the response for the ad service so i need to pass here employ and let's give it the code for 201 all right i hope this is going to work so i have created here that's fine and it's going to showing me an error because i haven't put any semi colon at the end so let's create air inside the postman right so in my case i have here the route which is add employee and it's have the post request so let's use here post request and slash api slash add employee and now inside your params you need to add all the values right so we have the name we have our email we have our salary right all right so let's add here a name of sarah and email should be sarah gmail.com and let's add up uh the salary of 200 all right and uh i believe that's fine right let's hit the send request and it's going to show up in error it says if i go to the preview it says add username to fillable property to allow mass assignment of app slash employer all right so i need to do here is you need to go inside your employee dot php model which should be there right so basically it's showing up an error here that you have your mass assignment error so in order to fix that up all you need to do is to first go inside your model let's give it your timestamp to be false so you don't need to create because if that's by default created so let's give it for now false so timestamps equals the false and i also need to add the fillables right to for the fillable you need to define your name of your attribute names right which is name email and the salary all right saves it and let's have a try all right so i click on this send and you see now we have our created so name email and salary and let's go inside your database and check this out this will work so now we have our new row inserted inside the employees table as simple as that right so the next thing i'm going to do is to let's update the employee data so update employee all right so let's hear use here the get request because we are getting the request for the employee so update employee slash because we need to update the employee a specific employee so i'm using here passing up here an id and let's grab here the controller name and the name of the method should be update employee right so update imply okay so i'm going to copy this and let's define a function here so public function update employee and now here inside the update employee i'm also have passing up here a request so let's give it here a request of this uh one thing i forget here i'm not using here the get request we have to use here put request support request is for the update because we are passing the request as well as including with the id so i'm also i have to update the request and plus we need to pass here an id right uh so i have passed the id with the request and let's write up a code so first we have to find the id so let's grab equal to employ i'm using here eloquent so let's first find the id which i need to update the data for so i i get the id and then i need to check if it exists so i have to use here a very similar fashion if it's null so let's pass here an employee so if it's null then just give it the response which i have already added up inside my get employee id so i am going to copy this out and let's put up here right and in case if we have the data so dollar employee equal to i all i need to do is to update here so update i'm using here update method so dollar request dash all right and let's give it the response of 200 right so i'm going to copy this and paste here this 200 means okay right so i pass a pair a response so let's check this out right so for the update case i'm using here put request and i i don't need that i don't need that let's update only the salary right so salary let's suppose give it the 100 right and we need to use the so update employee and let's first find out the id which doesn't exist 5 which doesn't exist so it says employee controller does not exist oops i forget to bring up the name of the method which i need to define here so let's give it the update stated update employee and now test this out uc employ not found and let's give it the salary right so i just want to update the salary 100 for the employee which exists so let's give it the for slash three and it says undefined function app http controller slash update so dollar employee equal to update oops actually i need to do here is dollar employee dash update all right okay so dash update and it should be very much similar to what we have right okay so let's have a try here and send it and now you see we have our id3 and with the salary hundred which i have updated let's do it let's make it 1000 and send the request and we have updated and go into our database for the testing and you see now we have 1000 as a salary for the id3 all right so it's very simple let's delete the id3 and now we have to write first the api for it so delete employee and let's write here the api for the delete so route call in call and delete so we're using your request as a delete request and delete employ so let's write here the controller name and let's delete the employee right so i'm going to copy this and let's put up here and public function delete the employee right so for deleting the employee all i need to do is to pass the request with the id and find the id if it exists so dollar employee equal to employee find the id oops sorry for that so find the id if it exists same case here if it's if it exists so show up the error and in case if it doesn't then just simply delete that so dollar employee dash delete so let's use here delete method so delete it and let's give it the response of 204 which is no content right so return response null and give it the response of 204 so 204 means no content right so let's first go to our delete request so define hit delete and slash api slash which i believe in my case is delete employee so let's grab the delete employee first define which doesn't exist and it says 404 not found which is fine oops no it's fine i believe slash api slash yeah i forget to bring here an id right so slash id because we need to delete the individual employee right so let's get and send the request it says employees employ not found and let's delete the user 3 right so now you see we have null data right it means the the employee is deleted and let's grab and click on the browse and you see now we have our record tree deleted all right so i believe you guys get an idea of using the api how to write how to use the basic api and with using that i'm gonna create an api and use it on uh on our angular which is a new series that i'm gonna create so i hope you guys will really enjoy this series and uh with the angular and the combination of larval full stack video right so thank you for watching and please subscribe and share it and i wanna see you for the next time
Info
Channel: AB Nation Programmers
Views: 11,217
Rating: undefined out of 5
Keywords: Complete RESTful API CRUD with Laravel, RESTful API CRUD with Laravel, Laravel RESTFul API, REST API in Laravel with CRUD functionality, Laravel API tutorials, API Resources from scratch using resource, What is REST API? | Web Service, Create REST API with Laravel | Create API in PHP, laravel rest api, laravel rest api authentication, laravel consume rest api, how to call api in laravel, laravel rest api tutorial, laravel api routes, api, API, abnation, laravel crash course, php
Id: vCt0M4bjkbU
Channel Id: undefined
Length: 27min 20sec (1640 seconds)
Published: Fri Nov 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.