API Development with Laravel 10 - Build a REST API from scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] welcome to this tutorial now in this tutorial we are going to be discussing about the the laravel API or API development with laravel so let's get started so now what is an API so an API simply stands for application programming interface which is used to connect or it is used to allow software systems to communicate and share information with each other so basically apis are crucial component in in modern web and mobile application so mastering API development will be a very essential skill for you as a developer so so here the goal of this tutorial is just to guide you step by step on how to create a functional and secure API using a laravel so by the end of this tutorial you should be able to create configure and test your own apis with that all right so I believe you're excited so let's get started so what you're going to do here you're going to create a simple API using laravel 10. and I will build a simple Cloud application that is create read update delete API for product resource so you're going to have a product website basically a laravel app which replaces different products prices and description and then you're going to use apis to to create a product itself be able to read update and also delete and basically that should be the starting point for when you want to learn about apis okay so here I've already installed laravel as you can see so I'm using laravel 10 which is the latest and it's very simple to install laravel you just need to come to your commands just run composer I believe it uh composer it's common so if you're using composer you just need to run this uh create that is composer then project and then I'll prepare then this and then of course here you just indicate project that you want to create so in this case this is variable then variable then of course API minus Democrat so that is what we have in there so you just need to have that and then once you are done with this then you're good to go uh there are other ways of installing narrowville but this is the easiest one you just need to have the composer and then you you run that command and then you're good to go you also need to have some because I'm using them within this application here so you also need to have it installed all right so let's let's continue so that is the installation and setting up your application so the next thing you just need to have once you have it installed you need this to come back and then go to your dot EnV file so Ctrl p and then put EnV and then here you can you just need to create the database and give it the name and of course the name would be whichever name you want to choose to give and then you're good to go okay so that is on database connectivity with the exam because I'm using them now the next part is to create a Model A migration and controller so in real in laravel we can create a model with migration file and control at the same time so that means uh here we can actually this is this is so let me show you so here we just need to run this command so PHP addition and then you're going to say mac model the model and then you're going to have a model is product and then here I'm going to have that Dash and then MC so this means that you can create the model the migration of course and the control at the same time so this is going to do all that work for us so when I run this this is on the application that we are in now what it has done it has created the model at the same time it has created a table for us which is product stable and then it has created for us the product controller that we need to use so this is an ND command if you don't know about it is a laravel developer you may want to note it down so basically it has created a product and then of course the other items so now once that is done let's just come back to our yes code so we have our controller we have our our model which has been created which is this one now within this model here what we need to do we need to do something here I will need to run this protected then available so what I'm doing here uh what I just want to fill my database with this this information because we want to do that we must have this in a book where you specify which items that you want to include in your database so yeah I'm going to harvest three of them so you're going to have um you're going to have the name description and the price like that and then and you save this so that's what goes with the within the protected billable and then once that is done we need to go to the tables so we need to type product so I'm using Ctrl V to do this So within your products um table we are so far once you run the migration command or create migration these are the two two entries that are created so we need to fill up others so for example we need to have above you're going to uh table and then string then here we are going to our name just the same way the same ones we have just added within the label and then yeah I'm going to add table I'm going to have text so the text is going to be description because we want to then outside there we're going to uh this is going to be nullable okay and then we have table next here you're going to have decimal and then we're going to inside we're going to indicate string price and then you're going to give it this to buy this here so the first one is going to be eight and then two then like that so what is going to happen there of course it's going to give actually this is the decimal places because the price of cost will have the small places or two decimal places given here all right so that's what we have so we have description um nullable and then so save that and then once you have you have saved this unexpected two sorry there should be up there yeah so let's let's run this command again and what what has happened is that now the table products table has been created within our database which is um which is API I gave it the name database to be API card okay so so that is done so we have successfully created the product controller of course we have rendered and then now we have created the product controller and of course the model so what I will want to do is to come back here and then look for the product controller the product controller is here and what you want to do since we are implementing a cloud application using an API I'll want now to to populate the controller with information so here what I'm going to do here within the controller the first part here we're going to have is the Public Public function is of course the index and then within the index there I'm going to uh turn products which is basically saying with that you turn all the products and then like that so what will happen here we are simply saying within the index function we want to return all the products within this uh event application so that's what we have here we need another one and of course we need to import this particular model because we are calling it now okay now next is uh we love public function we'll have another function here and this function this time this one is going to unload the store so you're going to app store I'm going to pass a request here I'm going to have that request first so once you pass those two parameters within the request is where now we're going to make the request for example within the store here we want to validate of course the data that we are going to enter into the database and that means we are doing a request so we are going to use and I believe this is if you are not new to laravel I believe you might have done this before so I'm going to have validated then request then within request I'm going to call this to validates then we are going to have this and then here the first part of course remember the credentials we filled within our table when we are creating so these are the ones we want to read to request for them to be validated before they get into our database so we are going to have this and then we are going to say uh required so we are going to have required then it's going to be going to be like this I'm going to use that sign there then Max going to be 255 like that and then you're going to have that comma there and then the next one you're going to have is the description so we're going to have description which is going to be this way then you're going to say required as well okay and then we have price we also going to have it there so we also going to have it as required quiet and then we have to okay so this is the and then you're going to say this this is going to be numeric because the value that you want to accept there of course should be a numeric value so we are going to have it that way so we need to end this one like this so we have this and then going to so there's no way we are going to allow somebody to enter data without it being validated as you can see so down there we are going to have a variable which is product and then here we're going to say product is equals to the products a model and then you're going to say create and then here we are going to call evaluated as we did there so I think that is simple to understand and then lastly you're going to you're going to return this this response as Json so we are going to have this this one then we're going to return this now this Json we are running is to show that we have successfully um that is the product has been successfully um ended and then we want now to get the the response in terms of Json of it so to show that the request has been made successfully so we are going to see that as we do it as we test so there's another function there so let's continue in the other function you're going to have another function here which is going to be the show function now within the short function here we are going to have we're going to return this to our return statement which is going to return response as well now within this response we're going to add here is since this is the show we are going to to return the response to show that everything has gone okay and the status okay so this is 200 status which means that here we call the variable here product like this you need to have a cover and then you're going to say 200 so 200 is just to mean that the state is okay and the message that the particular um that has been verified and then that has been created and then posted into the to our database so you need to show this within the show function here so that's what is that's what is happening there then next we have is public function which of course this one now is update so you're going to have this is updates so we are going to have a request as well there so we are going to have that request then here we are going to have so here what you want to do in case we want to update our data within this given application so I'm going to use this same thing because here we want to update our data again so here we're going to call the name and then here we are going to require of course the same case the same thing we did so yeah there's no need for more explanation and then down there we're going to have this we're going to have this product then I'm going to add this delete sorry not delete I'm going to come to delete you're going to have this as update which of course we are calling the validated okay and then down there we need to return this uh Json response so we need to return it here like this so you're going to show that the product has been of course we are now able to update our product so the last one is the one now that one for delete so since this is done so let's come back to let's come to now for delete we are going to have the public function and then they are the public function is destroyed so they destroy uh this one is going to take in since this one took in the request because we are doing the requests want to request for the given data the Destroyer is going to take in the variable of product is and product so here we want to identify a particular product and then we want to delete that given product so here you're going to to have the product and then you're going to say delete just like that then down there we're going to have going to have returned response and then here we're just going to say delete it which with the status of 200 200 is status okay now we also have other status for example you might have seen them for four four which means not found 500 which means internal server error and support so those are the different um different we call them HTTP HTTP status responses which of course they enable you to know what is happening if there is something that is missing so so that is uh so that is it so so so that is now the controller and that's that's basically what's that so we are ready to go and this is just a simple API application so let's go to watch laravel provides it is called API route whereby we can now of course create our API so here what you need we just need to create now a route for that um resource that we have created so here we're going to have no I keep it for getting that one so here we're going to have the resource and then here we're going to our products and then here we'll have product controller so we are calling the product Controller Plus so that now we can do our API request so so here we have the products their products so the API resource then products then like that I don't know what is missing okay so we need to import this class as well so we need to have that bus which is already imported okay okay so uh so now this is the this this is now what we have so far and now we are able now we are ready to to implement already now our API so for example we want to do several several things for example we want to list items within we want to to get a request we want to post a request we want to do several things so our controller is working fine unless there is anything that we might have forgot but we are still able to debug this now you need to have an application called The Postman Postman is a good um application that you can use when you want to develop your apis so this is very so this is a very good tool that you can use when you want to develop your API so you need to have it you if you want to test your apis when you are developing them okay so um so we can begin we can begin testing so what you need to have ensure your application is running if you're following up ensure it is running here we add this so we need to have we need to have this I don't know if it is running on yes it's already running here I believe everything is working well so let's just see if the application is running so it is good so this is our laravel so we can begin now what you need to do here for us to test our apis we can do the fasting here for example so we can list all the products so we just need to type the 127 actually basically is this one here I just need this because it's our localhost as So within our Postman you need to have it this way okay so so what you need to have here is um you just need to run this and then you need to append this with API since we have it when you go to our application you can see here what we have is um where is this API you can see this API resource and of course you're accessing it through the API so you need to have you need to append the API so what you need to have here you just need to have the slash API and slash products since we have products so the products we have is this one here so this is this so remember it is a resource and by resource I mean we have a resource I mean we have all this actually I should not have typed everything like sure like when you run the PHP Artisan Mac product controller with the resource it means that it will make all this index store sure update uh that is the update uh sorry is it update yes update then destroy all of them that are going to be met so what you're going to do is just to fill them up with the kind of um cred functionality that we are creating so so that's what I mean by resource so you want to list all the products all the products within our API so what you're doing here we're just calling this API and then you are saying list all the products now before we do that we can't list all the products because we have not created any products so far so what I'll need to do here uh let's see if we run this send request here what it is saying it is saying so what it is saying it is saying the departure method is not supported actually patch is for for um so this patch basically this is for updating so we do we do get request is the one that is done so you can see it is showing it is empty because there's nothing so we need to fill up this data here so what you need to do we need to go to we need we need to have so you can see before we continue so what we have here we have different um what you call them as HTTP HTTP request for example we have get which is used to get um whichever data you have for example within your database and so forth post this one is used to post data in other words to submit data to um to your database put is used to update together with patch and then delete of course is only to delete data so this um this that is their five of them are very important when you are developing an API so you need to understand them very well so that you can use them When developing for example now we are doing this one with a back-end language laravel now when you'll be having a front-end language like for example let's say you're using a react or you're using view or whichever front-end framework you'll want to understand how this works so that you can use them within your react applications or within your view or whichever framework you like so before we do that what we're going to do here we want to to we want to fill the data of this so what we are going to do here we are going to have you can see here we have something to call query params so this query params means that here you can insert parameters within you are given a Postman within this Postman so what you're going to do here we're going to have the first one which is name because so name because remember we have remember this is the kind of data we want we we have we have in our table so we have name description price so we have name then the value here I'm going to say shoe polish for example to polish and then I'm going to also have description of course because remember we have description and then within description here we are going to have shoe to polish shine just something so don't worry about all this so and then you're going to have price so price of that one let's say it is 10.99 remember we kept this one as eight to two with uh maximum of 80 digits two decimal places like that okay so we have get and here what we need to do we need to do Post because we want to post this data to our database so here when I click Send here let's see this okay so what we have here is that this data has been updated to our database and you can see what we have here just been updated with an ID of one there and of course we can fill other when before we do that we can of course inspect Within one of um so here we can so that is the password for the database so the database here uh use API threads that is the database and then select all from products and you can see this is now what we have failed within our database so that's what we have there so we can continue doing this and continue doing this so I can just come here and change this to something else so let me just do this quickly so we are just going to so just changing these values this price description and then you just have this one as post and then you click enter and then so or else we can still do this together so I just want to save one time so we have name here so the first one is this one oh sorry this one has just deleted the whole thing okay so let me just do this quickly and then good to go and so now I've added several products and you can we can see them here so all these are the products that are added through the API using the same method here so there are seven of them and I've added them through the postman the postman through the API so now we can do other things for example we can do the get request now they get request here for example we can do something like um so let's say you want to show a single product so with an ID let's say let's pick a product with an ID of three which should be peanut butter here so we can just come back here and do that using the get request so get request is just to to go to the database and then it will be able to get for us the specific product so and Define the variable products so I think we have an issue with our controller you can also test your apis through Chrome but this is not recommended the way Postman of course is always the recommended way but the reason I'm doing this is just for it to show me actually where the error is here so the error is on the show but it says the show within the show there is an issue of point so this is on the shore part here okay so uh so what we left here is just something we did not pass the variable that we are using within this function here that's why it is undefined and this just like we have here within destroy here we have this actually these error messages are good when you want to they make you understand something better so okay so let's run this again and see if it is about to pick for us the product so the product is this one here and that's what we have so you can also do other things such as um so that is uh obtaining us a single product or being able to show a single product you can create a new product of course that one you have been able to do now you're also able to update a product so for example you want to update a given product so you can either use put or patch so for example if I use put and here I want to update this product which has which has an ID of three which is of course this one here so this pin at the bottom let's say I wanted to you change it to something different so how would I go about it so basically uh you don't need to type everything you just need to specify for example you want to change the price so all the name the name and the price or the name and the price so this is this was this is uh this is the bread butters so let's say peanut butter so let's say I wanted to remove the peanut a butter so I'll have this as peanuts like that then set the price which is this one here so the price I'm going to write it to 149 sorry yep so 149 .99 so we have that and then the method here is put let's see so we have an error message of description field is required so it means they also need the description because we are using the the validation okay so unless also a bit okay so this is undefined so we also forgot I don't know what okay so let's do this again okay so the the product has been updated and as you can see here what we have is also the same thing that is also reflected here so when we run this again we also have the same thing here so that's that's how we go about that is a simple way of implementing apis and of course you can also delete so for example we can also you can also delete that particular given product so for example this is with the ID of three so if you call the delete assuming we defined the products so to return this statement message which of course we highlighted here which is this one here return with this response of deleted with 200 and you can see the 200 is this one here which says okay all right so now the last thing here I want to do is to show you how to set authentication in your application now since you have now started with your API and everything is working well your apis are good and of course you are now to use a front end framework or something like that so how can you use authentication also to ensure that your apis are protected and your application is as the security that you need so implementing this authentication within laravel there are different ways you can use one of this that is to handle what we call Api authentication so for example one is laravel Sanctum another one is passport or JWT that is just one web token Authentication um which are that the party packages of course but the best one that I would recommend for now is laravel santam which is provided by laravel itself so what you can do here let's come back here so go to your your API resource within your API route down here I'm going to have this route which is middleware to middleware now okay and then here we're going to have the out so the out here we're going to have is sanctum I spell it correct and then here we're going to have group so what you're doing here is basically when you're grouping your you are out you want to place them within a group in laraveen so this is how you may go about it so we have the function there and then we're going to have we're going to have this close with this so what you have just done here we have implemented the old Santa within laravel which is now going to ensure that before you access any um any API within this product controller it has to be authenticated that's what so so let's now Implement our Center within our application so so how do we go about it so this is how we go about it so since we want now to ensure that our our application is able to use random so down here we are going to have this going to our routes post and then I'm going to have this going towards and function are going to pass it with the request question okay and then we're going to have this request I'm going to check we're going to validate check if of course we have the provided Angels so here we're going to have amen okay you're going to have the required Acquired and then required email then we are going to add password we also going to require so in other words they are required so they have to be there when you are authenticating so here we are going to have we're going to say the user here is going to be user because to user sorry this is a user so user aware that is aware email so we have requests then we have email I want to request for email and then this is fast then we have something like that so afterwards this we're going to have to check okay so after the user we are going to check if the password of course is um we're going to check if the password we're going to request for the password just like we have said the user where the email request demand and then here we're going to have an if statement which are going to say if the user is not a password we asked check and then went to our request password and then we'll have here is user password so what are we saying here so we want to check the user we want to check we want to determine if the user password is is in Ash form so that's why we have this here like this we are going to check the request for the password and then requesting for the password we are checking a user password if the password is now if that is not the case you're going to draw a message here so we're going to have this modification exception is then you're going to say with yes and we're going to have something so what you're going to say here is that provided changes right so the provided credentials are incorrect like that and then this one you're going to end it like this okay this one you're going to have it that way then of course here we are going to close this and close this with this one yeah like that we have this as this so finally we're going to say return user and user create okay then inside here we're going to have requests device so I'm going to tell you what we mean by this but basically we're meaning that um a person to be logged in fully needs also to input this device name that's what we ask in therefore so this is going to be in plain text token that is the the token that will be generated here so now what you need to understand is this is how this is how the Phantom works this is how sometimes works so we are going to return user create um an arrow so create the token uh device name so you're going to request for the device so it's going the person is going to input a device okay and then um what we have here so we're going to say the user is modern I mean from the user table and then we have where email and then request so we are requesting the email sorry so we are requesting the email and then passed like this so here this is going to be so here you need to import this class which is that one there you need to have it that way and then so the email is required actually what I was trying to do earlier I thought this the outside that will just generate the required email but we need to have this is like this so that um when we we do a request we are actually going to ensure that the person has provided the required credentials okay so um so now what are going to happen we are going to make a post request to this I believe everything is working okay okay guys so once you have implemented yours your laravel sometimes like this now you need to have somebody who is already logged in within your application somebody who is already registered within your within your application that is within your login registration and so forth now laravel comes with with this by default so for example we have um you can create somebody just by running the DBC of course and then within this user Factory here I'm able to to get a fake name a fake email of course a password which I just uh implemented here and then something just other additional information of course within this Factory here so this person is already the person who is already authenticated in this application which I ran here is Brian and then the email is me that is my email so what happens here is just when you run the DBS DBC or DB uh PHP artisan user DB that is user seed what will happen is that the database is going to be populated with a username an email and of course if you want to check that you can just go to your terminal and of course go to the database itself and then you're able to see that what the person that we have here is is brand and then of course the password is in a hash format but the password of course it is password so what you need to do here once you run this uh API sanctum token because this is the API that you are using remember from um from where from our API Dot PHP this is what you are this is what you are requiring to create or come up with so this is the so yeah we are we are going to the post method and then we are using the API that is sometime token whereby we request for the email and the password and the once that is done we also request for um for the device name post so those three now if they are true we are going to have a token that is going to be created this token is the one that you're going to use to include it in the header of subsequent requests that are made within this application that is in the edit key of the authorization of your application here you can see here within your authorization here we have the better token so this is better token we are going to get a token and then this token is the one that you're going to place it here so that anytime that you make um an authentication in this application your app is going to check this authentication um and then this token of course and then if the token matches uh the person is going to be allowed into your application so what I'm going to do here I'm going to add the email so email then this email is Brian at gmail.com we're going to have password then password is going to be like this and then we are also requesting for the device name although you can stay request for the past two and if you don't want the device name adjust for security purpose that's where we have included it there so we need to have the device name there now the device name is this one here when I run hostname within my Linux I'm able to see you can also do it on Windows and check so the device name is that I'm going to copy it and then paste it there like that so once uh this is correct according to our application here uh the where is it so this is what we have so according to this and then the password of course is within the factory so what you're going to do you're going to run this send using this post because remember the route the route of our application here is a post request that you are making so you just need to run this this now once you run this what you're going to get is a token so this token you're going to use it to to add it to your subsequent uh edit request that you're going to make within this application so you're going you're going to authorization better token uh if it's not there just choose so we have others but we are using better token and then you're going to add that token there like that so that now once you come to come back to what come back to products now here you are able to access products because now it's allowing us to this that these are just validations within our of course our application but we can let's see if you're able to access so are you even doing you're doing post should be get so let's get the first product you can see this is the first product here but what would happen if this uh this this token was not there and then we tried to make a request here so it will tell you message is out and authenticated because um the token is needed yeah if I still have the token so we do still do again send and then you are able to access so this is the secure way this is the recommended way to implement your authentication using larval Center so let's meet in the next video it might have been long but hope you have got something so like this video share it with your friends subscribe to this channel
Info
Channel: Web Dev Trainee
Views: 15,933
Rating: undefined out of 5
Keywords: API Development with Laravel 10 - A Comprehensive Guide, laravel api, laravel sanctum, laravel 10, web development, laravel api tutorial, laravel 10 api tutorial, laravel crud app, laravel api crud app, API Development with Laravel 10 - Build a REST API from scratch
Id: BVNgCzt2pjY
Channel Id: undefined
Length: 47min 33sec (2853 seconds)
Published: Sat Jun 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.