Laravel REST API Tutorial - Build an API in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you're going to learn to build a very simple restful api using the laravel framework we're also going to take a look at a vs code extension that allows you to very easily test your api endpoints so you can not only build your api in your editor you can also test it there if you guys want the source code for the project it will be available at github so feel free to clone it fork it and update it for your own project okay so i have a fresh installation of laravel and if i head on over to my browser it's accessible through this url and you can see that the layerable version is 8.21 okay so the first thing that we need to do is open up our emv file and change the database credentials so i'm going to say restful api as the database name for the user i'm going to say root and for the password secret if i save this file and open up my terminal i can run i can i need to actually create the mysql database so mysql the issue root p enter my password and i can run the query create database and the name of the database is restful api and let me exit out of there and now i can run php artisan migrate cool okay so now we need a model so you can create a new model using this command php reason make model and the name of the model is going to be called post and we will we will also need the migration files this will create the model as well as the migration file so let me open up the migration file it's called create posts table in here we basically define the database columns so what we need is a string of title and a long text of content so a blog post in our system consists of a title and a content okay so that's what happens when you run this migration it creates a post table with these fields and when you reverse the migration it drops this table so next we need to run this migration we can do that using php artisan migrate right so now we can actually get started creating the various routes for our api so let me open up the sidebar and open the routes and api.php file in here here is where you can register api routes for your application so i'm just going to add a new route and it's going to be called slash posts and what this will do is basically return all the posts but we need to import this post up at the top so use app models post so this post eloquent model lives in app models and post.php so now we can actually test this in the browser so one thing to take note of is that when you register routes in api.php the routes all the routes are named spaced under api so it's gonna be your website slash api slash whatever you registered so slash post and you can see it returns an empty array because there are no posts in the database so if we wanted to create a new post we can php origin tinker and then say app we can say post equal new post and then say post title equals my new post the post content equals my post content and then say post save okay now if we refresh the page you can see a new post has been created next we need another route to actually create a new post so route it's going to be a post request to posts and what this will do is basically run post create and then provide the list of the columns so it's going to be title so what do you want the title to be we can just say request title so it will get the title field from the request body and then we can say um content and it's going to be the content so this won't work just yet and we can try this and actually to try this we can use something like postman or we can use a vs code extension let me open up extensions and it's called rest client and you need to install it i have it installed already so the way it works is you create a new file and you save it as dot http i'm just going to put it on the desktop so test.http replace that this is the the syntax you can use you can say get and then the url which is um this one right and then you say http 1.1 and that's it and you can see vs code will add this link or a button that says send request and when you click the click that you can say the response will be a response and the headers uh will be displayed here now you can all you can also do post but before you write another you need to type these three hash symbols so next you can say post and http restful api dot test api post http 1.1 and then you can provide any headers so it's gonna be content type uh application slash json and then you can provide the json data so it's gonna be title it's gonna be my new title um that one already exists so we can say my second title and for the content i can say my second content let me save that and send request okay so it fails it says mass assignment exception and what's happening is laravel is trying to protect you uh against certain type of attacks and so uh the way to fix this is to go to the go to your model so it's post.php and just add a protected fillable property and say hey i can mass assign title and content so you can save that and run that again okay so title cannot be null oh this is the wrong json format this happens a lot if you um constantly switch between javascript and php let me send this quest again as you can see the new post has been created but what happens if we if you don't have a title for instance it's going to say it's going to fail and say hey the title cannot be null so the way to fix this is to actually use validation so you can say request validate and then provide all the validation fields and so you can say title is required as well as content so now if we send the request what happens is tries to redirect you but if you're using um ajax it will send you the appropriate response that a title is field is required or whatever the next step is to so now we can um get all the posts and we can create a new post let me title and say my third title and let me try that again just to see if it's working properly and it is so now we've created a route to get all the posts and we've created a route to create a new post now we need a route to update a post so that needs to be put and put slash posts and then here we need to provide the id and so we can say post function so this id will be available here so you can say dollar id and in here we can say post equal post find or fail and then grab the id but if but laravel actually makes it even easier for you if you say post and then use post here and type in that we're expecting a an eloquent model but what it does is it automatically does this for you and returns the post in your argument so all you gotta do is say post update and very same thing title is request title and content you can save this but if you want the validation as well you can just copy paste this cool i'm missing a semicolon here let me save that okay so now it's time to test this so we'll just say put and actually before we can create a new one we need the three hash symbols and then we can say put http restful api dot test api posts and then we need to provide a post id so let's say that we want to edit the third post so we'll say three and then we need to provide content type and kind of type is application slash json and let's um provide a title so it's gonna be my third title and i'm just gonna say update it and same for content and let me save that and send the request so it doesn't return anything but if you get all the posts you can see that the third one is updated if you want to return something you can um just say okay return you can just save this as a variable it'll return true or false depending on whether or not the the update was successful or not we can grab that variable and then just return and say success equals success so i'll say successfully was true if the update was successful and it'll say success equals false if the update was not successful so let's try that again oh actually this will just create a new post um we need this one so send request and say success equals true all right so we've successfully created um endpoint to create to get all posts to create new posts and to update an existing post and now we need a one to actually delete the post so that needs to be route delete slash post slash the id of the post so we're gonna do the exact same thing and grab that from up here so i'll say uh post and we can say post delete and then um you can save that here as well and then say turn and success equals true and you can obviously um update the response however you want but i'm just going to stick with this format okay so to test this let's add another one delete and say http restful api.test api post and actually you know before we do that let's just um grab all the posts and see okay so let's delete this one because uh it says id4 and it's uh the it just says my third title so let's remove that one delete http restful api posts four okay um well i guess we don't need the http after all so we can just say this if you send the request okay it fails oh it's it needs to be restful api sorry about that let's send the request again and say success equals true and so just to make sure let's grab all the posts and you can see the fourth one isn't there anymore everything's working perfectly so we can um get all the posts create a new post update an existing one and delete the any post so now we can just leave it at that if if that's the extent of your application that's fine but let's um actually extract the controller because um more often than not you don't want this in your routes file i'm going to generate a new controller say make controller um let's say posts api controller so controller was created successfully let's open up that controller posts controller post api controller and let's add a public function index and say return post all here's your post hall and in order to replace this we have to say posts api controller class and then the action name which is index so let's import that at the top app http controllers and post api controller let's save that and i think that should work let's try it out so that's that http let's go up here it's on the request okay so based send the um request to slash post to get all the posts it says um controller not found so class app http controllers post not found it's because we imported the wrong thing so let's go up here um no because um we didn't import the right class right model in the post api controller so all we need to do to fix this is say use app models and then post next we can um next we can start adding the other routes so route post slash post and this is going to be post api controller class and the method is going to be called store so if you save this we can just copy everything from this method and delete this go back here and say public function store and then paste everything here and it should work as well let's add the other methods before actually testing them out so post slash post and this need to be the put request and the message is going to be called update so let's copy everything from here delete it all together and then come here and add the method called um update and paste everything here so we do need this post variable so we can just type into here post and grab it finally let's uh do the same thing for delete it's going to be called public function destroy and we need the post variable and instead of this closure here we can just replace it by post api controller class and then the destroy method let's save that and let's go ahead and try testing all these out so if we add a new post select send request and it's created so let's add let's update this route so post slash 4 my brand new title updated so let me use a request it says success true and finally let's just delete this one so for delete and that will say true and if we grab all the posts you can see that that one is deleted [Music] you
Info
Channel: TheCodePro
Views: 45,169
Rating: undefined out of 5
Keywords: laravel rest api tutorial, laravel api tutorial, laravel restful api, laravel api, laravel rest api for beginners, build an api in laravel, build an api in laravel 8, create a rest api in laravel, laravel
Id: WDha52dbLWM
Channel Id: undefined
Length: 20min 44sec (1244 seconds)
Published: Sun Jan 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.