How to Build API Resources | Laravel API Course | Learn Laravel API | Laravel API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dory and i hope that you're having a great day now that we have set up pretty much everything that we need i want to start off building our resources so if you're interested in supporting the channel and you want me to continue on creating tutorials i have created a patreon where you could get subscriptions and you will get benefits such as a private discord group where we could help each other out it's pretty difficult to maintain all the questions that i'm getting through instagram and youtube and even though i'm trying my hardest to respond to all of you guys the private group on discord will be very beneficial for you since it's a community where we could all help each other out whenever i write an application i usually write some stuff out that might be useful during the build up of the application if we brainstorm a bit about the application that we're going to create we obviously have books since we're going to create a bookstore so let me add some comments over here so a book belongs to an author right so we need an author's resource so what more we have a user that creates books so where do we start with let's think about that for a second we can start off with a user's table since the books are not implemented a book has a lot of different relationships which makes it a bit more complicated to start there on the other hand we have authors an author does not need a book to start with so in my opinion we need to start off by creating the authors and then the books right now we are finally ready to write some code and since we already have everything set up i want to start off creating a model and set up our database so let's hop to the cli so item and in here so inside our bookstore directory let's write down php artisan make me a model and a pretty cool flag that i haven't talked about on this channel is the dash so space dash a which will create a model database migration factory and a controller for us i also want to add another flag which is the space dash r flag and this will make a resource controller and this is pretty important since we're going to create a rest verb from the beginning so we need to give our model a name so let's say space author hit enter and as you could see our model factory migration cder and controller has been created the issue that we have right now is well let's up to visual studio code and let's open our controller is that the controller is singular and this is not the best practice to use for a controller so let's open it and let's change the class name to authors controller and let's rename the file so let's say authors controller so plural for now we don't need the author's controller so let's save it and close it off and let's actually get rid of the two comments as well we are ready to set up our database migration right now what we could do is to open our database folder migrations and we need a last migration because it's the last created migration let's open it and right here we have a schema where we're going to create the authors table i want to keep it very simple right here we do need an id and in between an id and timestamp let's add a new column so table let's give it a string and the name is name so the author's name the auto increment id will come from laravel itself and the timestamps as well so we don't need to touch anything else so let's save it let's close it off the last thing that i want to do is to open our author model so let's do that and right below our use factory let's say protected variable fillable is array and we only want to pass in the name adding the protected fillable will make sure that we can add the name when we use a create method on our eloquent quest since it's mass assignable right now so let's save it and let's close it off and i think that we're ready to migrate our database so let's open item let's write down php artisan migrate let's hit enter and as you can see the authors table has been migrated a couple minutes ago we also pulled in a factory as you could see right here so a factory has been created and what i want to do is to use that to add some test data so let's go back to visual studio code inside our database folder we have a factories folder and let's open the author factory inside the definition array that we have right here that's being returned we can add a complete list of attributes that we want to generate with our factory in our case since the id and timestamps will be auto created we only need to say that our name so in single quotes is equal to this the faker class name now the faker package that we're calling right here can be used to generate fake data for emails phone numbers addresses names and way more so it's pretty cool in order to call our factory so our authors factory in order to call our factory we need to have a database cedar and as we can see in the cli the seeder has been created as well right here you can see a folder called cders let's open the cds folder and in here you can see two files we have the author seeder and the database seeder we need to start off with the database seeder.php file so let's do that and let's get rid of the comment that we have inside the run method which is the only method that we have right here we need to call the author seeder that we have so let's say this call it and inside the parentheses let's say author cedar column colon class we're not done yet let's save it first because we need to open the authors seeder and we need to do something inside the run method of the author see there as well first we need to call the model that we want to associate our author seeder with so obviously that's the author model so let's say backslash app backslash models backslash author colin collin then we need to call the factory model so let's say factory parentheses which accepts one param the param is basically the number of rows you want to create so in our case let's just say that we want five otters and in order to create the rows we need to perform an axis operator on the factory model and call the create method some of you guys might probably think why are you adding the entire path inside the run method we could put it in inside the use statement as well but since we're only going to use it once we could write the entire path inside our run model let's save it and we're ready to perform a migration and seed in our command so let's hop to the cli and let's write down php artisan migrate space double dash seed let's hit enter and right now we're having an error message so let's go back and let's see what's going wrong right here let's start off with the artist factory i think i didn't save the file so let's do that let's go back to i term and if i perform the same command again you can see that our database has been seeded now to double test it let's go to mysql let's say select everything from the tables that we just created called authors let's hit enter and we added five authors in our database the data that i have on my screen right now will definitely be different than yours since these are all randomly generated now that we're done with the database and migration we are ready to set up our routes so let's go back to visual studio code let's close off the author's factory and boats cders right under the route that we created right here let's create a new route so whenever you want to pull in one specific author your endpoint will be author forward slash the specific author id so let me add that this is the route for one specific author so what we did in the previous episode was showing data through postman if you want to show data in your ui you need to make sure that you reference your author's controller just like we used inside the web.php file we're not going to create a route outside of our route that's a mistake on me because we created a group route in the last episode and the biggest advantage is the fact that we can make sure that all routes in the group are going through the autocollin api middleware it's also good to define your api version what we did so the version one and this will be added inside every route that we need so let me show it to you right outside of our second route let's say route column column get and first we need to pass in the pad so the end point so let's say forward slash author forward slash curly braces author then we need to define the controller what we want to associate our author with so let's say brackets otters controller column column class and we can define the method so let's say comma space single quotes show now the show method that we have right here has been made when we created our model since we added a resource flag to it so let's open it let's go up and in our controller we have the index create store and the shell method so let me save api.php well i actually can see that i made a typo right here because we want to go to authors let's go back to postman and if we create a new route we need to add our bearer token and all the other data that we have again so what we could do is to click on test request now let's say duplicate let's rename it to author and we actually don't need the test request anymore let's change the end point to four slash api four slash v1 forward slash authors forward slash one so the one represents the author with id number one so let's click on send the body is empty but the return status that we have right here is 200 so it is going right but something is also going wrong so pause the video think about it for a second where there are no authors returned back even though we created five authors through the factory if we go to our api.php file right here you can see that we're calling the show method inside the author's controller so let's go there and as you could see our method is empty so we're actually not returning anything so what we could do is to test it out so let's say return single quotes test author save it go back to postman send it and as you could see test author has been printed out or to even make this better we could say return the author object that we have as a param save it go back to postman and let's send it one more time and as you can see right here we pulled in all information where id of an author is number one let me go over the flow one more time inside the request uri we're saying that we want to search through all authors and find me an author where the id is number one then inside the route so api.php we're defining a route to forward slash authors forward slash a specific id a random id or whatever search through it and then go to the show method inside the authors controller and send anything that's returned back to the user in the video where i talked about adhering the json api specification i showed you that attributes data and types are very important and to be honest that's the actual structure that i want to teach you before we get used to other stuff or the wrong stuff so the first thing that i want to do is to show you the simple way which is not completely correct but it's good to start with first things first let's get rid of the author object and let's return a response access operator json inside the json method let's add an array so brackets and in here we need to create a data member that will contain the resource object data that represents our model let's say in single quotes data is equal to another set of brackets we can set up the way we want to see data inside postman so first let's grab the id from the author object so author id comma we can define a new type which is a string of otters for the name and timestamps it's best practice to place these right inside attribute members and in order to do that we need to create an array inside the data array so let's say comma attributes is equal to an array and in here we can say that the name is equal to the author name and let's do the same thing for the created ad and updated that so created underscore add is equal to author created underscore at updated underscore at is equal to author updated underscore at all right let's save it let's go to postman send it and well you can see that data has been added but let me restart my postman for a second because i don't like the way it has been printed out all right this is the pretty way i don't know why it just didn't work but it does right but as you can see we have data which we defined ourselves we have the id type we added attributes so an attribute array ourself with a name created that and updated that this is not it and this is mostly step number one the best way to do this is to create an api resource so we could easily define how we want to transform our models into api resources so let's create a resource let's go to the cli let's write down php artisan make me a resource called authors resource let's hit enter and our resource has been created successfully so let's open visual studio code and right inside our app folder we have a new folder called resources and there's a file in here called authors resources so let's open it you can see that it generates a two array method right here which takes one param and that's the request object and it needs to return an array if we go back to our authors controller let's copy everything that's inside the data so let's copy this part so the id to the updated ad remove everything inside the return value add a set of brackets and let's end it off with a semicolon in here let's paste it now the reason why i don't copy the data member is because it will be automatically created with the two array method you can see that we don't have the author anymore this needs to be changed to this which will refer to the request so let's do that everywhere save it go back to the authors controller and obviously we don't need to return this again so let's get rid of it and we don't need the two brackets all right what we need to do right now is to refer to our autist resource that we just created and before we do that we obviously need to pull it in so let's go at the top of our page let's say use app backslash http backslash resources backslash authors resource now let's go back to the show method and let's return a new instance of the authors resource we need to pass in the author because the author's resource accepts a request so let's say author and let me actually save all files all right let's go back to postman let's click on send and as you can see the same information has been printed out on our screen but we clearly cleaned up our codes now before i wrap up the video i want to show you one more thing that's important to remember if we go back to our author's resource you can see that we're passing in the id of an entity you can see that we're passing in the id as an integer because it's pulling in the id which is an integer from the database if we open postman one more time you can see that the id is an integer as well but according to the json api specification ids must be a string now in order to change this up let's go right in front of the keyword this let's add a set of parentheses and inside the parentheses we can define the data type so in our case let's just say string save it go back to postman click on send one more time and we have converted our id to a string in the next video i want to dive into research collections and with that being said this was it for this video if you do enjoy my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button you
Info
Channel: Code With Dary
Views: 20,107
Rating: undefined out of 5
Keywords:
Id: L1owEfA9ioc
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Wed Apr 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.