Laravel - Day 8 - Resource Controller Introduction and Usage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello what's up everybody how is it going welcome to another video of tutorial series learning laravel from scratch I'm your host Andrew Ali and I'm here today with a new topic and the topic is learning resource controller in laravel resource controller is a really important topic in laravel that helps us to minimize our code so much that we don't have to write routes for each and every URL we make in the controller like we are doing right now at the same time managing the resources by meaning resource I'm trying to say you the object of the Moodle becomes so easy and storing the objects and editing the objects or let's say saving the objects deleting objects become really easy it really minimizes the code but I want to discuss about the complexities if we don't use this source controller before we start with the resource controller so if we are not using resource controller let me just quickly show you one of the examples without using the resource controller so this was something that we did last time so if you go to admin localhost project public admin we had created categories and linked for the categories we had created a page where we list our category and we were about to learn add new category edit delete today so I want to create another page and let's say let us open our controller first our controller was categories controller so let's create another function over here and maybe call it detail way we want to show the detail of the category okay so we want to show the detail of a specific category so I would like to pass an ID over here okay and what I want to do is I want to grab the ID with this ID grab that entry of this ID from the database of the category and display its title let's say so in order to do that what I want to do is I we'll have to write let's say create a variable and it is just call it cat equals two so category now if you want to grab an array a group of everything and that is in the database we call a function called find I mean all but if you want to get a single and let's say entry you just call fine and pass in the ID over here so this is our ID so I'm passing this ID in find so it will just return me a single row now so category category you find with this ID will return a single entry of value so that we don't have to use loopy anymore now what I can do is I can just say echo dollar cat title let's say I want to display the title of the category okay so this is our function now next step what I have to do is I have to write the route for this function right here is our route inside a web let me write another route for this so out and get admin / categories and it's a detail and we are passing the ID into the URL so we need to pass some ID through the URL so that we recognize which ID we want to see or which news we want to see so in order to pass any parameters to the you are and what we need to do in laravel it is we just give a slash and place our let's say segment over here this way but we don't know we don't give dollar in front of this so whatever is the name of the parameter in our function we give the same name over here the segment should be the same name so we are passing the variable as dollar ID so then the name of our segments should be idea over here as well okay so this means categories controllers at the rate detail function right let me just call this let's say detail underscore category okay so this is our function let us check our database before mm-hmm so let me just quickly check my database first so localhost last PHP my admin' and my project is project inside my categories I have ie number one and ID number two so let me just manually browse so we have created a route for detail which is admins last categories last detail and let me pass one of the ideas over here and it's a one or two let me just pass two so if I pass through this means I am passing I'm trying to get the entry of entertainment right and I have a code and the title of the category so entertainment should be displayed right it should be detail in turn it is saying pass in in my routes let's say what we made a mistake if you go to a web web route get we missed a slash over here slash admin / categories last detail slash ID comma categories controller at detail bracket ends here and my name we miss the name name is detail category okay let me just refresh this so we got the entertainment of here so basically what we do is whenever we create a function in our controller we have to write the equivalent route in our web so let us say if we have ten functions in a controller then we have to write ten routes for each and every function but generally what do we do inside a controller we generally perform some crude operations along with let's say detail page or listing page right so generally whenever we create a controller module we list it out we create a new moon and we save that we save that model after we in let's say fill out the create form then we and let's say edit the existing model so we load the content of that model in an edit form and we hit save so we update the model and we delete the model and sometimes we might want to view the detail of the model so these are some four or five functions that we generally do in our modules right so laravel has made it easy by creating all these five functions in the same controller and sometimes we also delete the model right so all these five or six functions and that is related to the crude operations through a resource controller and managing it by single route we don't have to do anything about it we just write a single route and mention that this is the resource route at the same time we create a resource controller and all the route is already been covered by a single line of code and at the same time all of the functions are auto-generated we just need to type in the code inside each function but the function is already there at the same time it handles the type so right now we can see we have everything is good but sometimes we want to post things while while we are saving a content from create form we generally don't use gate we use post so in that case we need to create a route for posts post similarly when you are updating something or we need to use patch or put sometimes similarly when we are deleting some entry we need to use delete type so we have gate we have post we have put and patch so we have delete so we need to use all those things in different scenario but using the resource controller we just write a single line of route that will take care of all those things we don't need to worry about boot patch delete get post okay so let's see if you go to the documentation of laravel and search for resource controller you will find resource controller under controllers topic so let's go over there so this is our resource controller so whenever we want to make our controller resource controller we just need to add - - research resource or - - resource at the end of their command and this will create a resource controller for us at the same time whenever we need to write the routes for all the functions generated by resource controller we just need to mention this one single line of code in our wave dot PHP inside routes and this line of code will take care of all the routes so what I want to do today is I just want to backup my categories controller so just rename this rename and maybe add an underscore at the end so that we can reuse the code let me just close this and remove all the routes related to the categories because we are not using this anymore okay now what I want to do is I want to create a resource controller for categories so let me just open my terminal now need to go to the folder of the project so applications amps www project this is my applications it's Jens am some project now I need to create a controller so PHP RT son make controller the name of my controller would be and it's a categories controller category is controller and this would be a resource controller so - - resource okay it inter controller created successfully previously we had a blank controller like this let's see like this just test but if you now open a resource control that you just created which is categories controller you will see all the functions has already been placed over here the rounds for these functions has already been mentioned in if we just copy this line of code and paste it over here this means route resource instead of photos we should have categories so categories and photo controllers should be category categories okay so categories will be our routes all the functions related to categories will be covered by this single line of code and our controller name would be categories controller okay so this was one piece of line of code we'll take care of all these functions we don't have to write around for each and every function anymore now let us discuss what these all functions are about so index is generally generally used to list out all the it's written over here display a listing of resource so whenever let's say we are creating a resource control for categories this means index function will be responsible to display all the categories similarly if you want to create a form in order to create a new category we'll just use this function because so the form of the creating a new resource okay this is related to the form now after we hit submit in that form it will post some data to some function and that function will be store so store is a function which is used to store the newly created and category in our database this is not for update this is just for create so after we hit submit button in our create form it should submit our button our form to this function similarly we would like to let's say source so is generally used to view the detail of the detail of something so this is the function that is responsible to review the detail of some let's say module similarly we have edit it is just like create but instead we have to let's say pass pass on some value to the view so that we Auto populate the existing entry in our form in the case of create we will have a blank form while in case of edit we might have let's say a form filled with pre-existing value in the now after we hit summit in the Edit form it should update our into database and that will be taken care by update function similarly we have last one which is destroy which is used to delete the entry in the database okay now if you go and see there are some of the actions let's say some of the let's say type or submission type of an action so index it is saying that index is respond is used as get because it is grabbing entry from the database and just listing it so create is not is just a form which is being displayed in a browser so it is also get but after we it's submitting that created it takes us to store so it is posting some data to the action so it is its type is post so now whenever we try to view a file we do nothing we just grab the interesting database and display it in a browser so it is get similarly while editing it which is just Auto populating increasing database in the form so it is also yet but whenever we update it it is not just a simple post in this case while storing it we are submitting the entries are form to the database so this is known as post but in case of update we are doing the same we are submitting an entry to database but at the same time we are adding an ID to to make it clear that which entry of the database we are about to update so we are patching or putting an ID along with the array of the form values so such scenario where along with the form values we will be use an extra field that is ID in order to save entries to another database in that scenario is known as put and patch so only difference between this and this is this directly submits the values form values to database and create a new one but in case of update we need to be explicitly mentioning that which entry of the database we want to update so we need to patch an ID along with the form values so such scenario is known as but--and batch okay and finally we have delete so delete while deleting any entry from database this could be a little bit let's say confidential or secured information or secured or secured acts and that we don't want to allow everybody so we we have a special submission type known as delete so if we try to access this URL using gate or post or put or patch it is not going to work we have to explicitly mention that this type of submission is of delete type only then shall it work okay similarly and we previously had created whenever we worked on routes we had created names of each route but since we don't have name of each and every function of this route so name we should follow this convention so whenever we are trying to access the index function of a controller will write the controller name and dot index so in our case it could be categories dot index so if you are browsing the create function will be used using categories create for store categories not store for show category start so and so on similarly you can see that previously we had a URL and that URL was let's say represented by a route name but now in this case you can see there are same URL being interpreted for example this and this has exactly same URL and you see that but this is this and this is being separated by the and it's a submission type so this is get and this is post so in case of routes in laravel even the submission type or route type will also make the URL completely different so if we have admin which is of type get that is a separate route and if we have an admin which has type post that is a separate route so in the same way we have here two URLs exactly same but the submission type is get and post so they are both completely separate route and representing completely separate functions okay so in this case it is representing index function while in this case it is representing store function similarly you can see that photos / photo so this could be ID or this could be anything slug anything so if we have photo / something this means get and this represents show and if we have photos / something which is at the same as this but if it is a put or patch type this is representing update and if we have photos and as something the same URL but if the summation type is delete it is representing destroy function so these are some of the basic thing that we need to know just we need to be careful which name to be used which route name to be used where and what are the submission types of which axons and how to use them so this was a basic introduction of routes will be trying to use and the resource controller for some of our functions that we did earlier and continue and the topic later on so let us let me just complete the first thing that we did previously so our public function index is equivalent to what we did last time so if you go to categories categories controller back of genitive just created we will just copy these two lines of code which was actually grabbing all the entries from categories table and sending it to the view file this way so this is one of the way in order to send the values to the view file or we could also just say comma and pass the array over here like this okay this could be also be another way of working with the view file now currently I will just use with so we are passing all the categories to admin categories and index now second thing that we need to now modify is if you go to our layout resources views layouts and admin blade if you see here in our admin it is now throwing us error because we are using a route in a sidebar and which was list category which does not exist anymore because we had created a route called list category in order to generate the link in our sidebar for category but that does not exist anymore now in order to list the category the name of the route that we need to use is photos less index or categories less index right so if we go to see our admin blade in the block for meaningful category we should not use list category together we should use categories dot index as suggested by our resource controller documentation so this is the route that we need to use this is the controller names in our case it will be categories dot index okay now a field after we place in categories dot index this should work fine see okay now if you click on category it is saying it is not category it is categories yes so it is saying class AB HTTP controller category not found in here okay we have not used and model yet so use app slash category so after we've done this use apps last category and we also maybe need to copy our constructor from our backup so that we check the session and we do the session handling before browsing any function now let us try to refresh this so it is listing our category now our you are this public slash categories which is working fine I hope you understood how to use the resource controller so we will continue with the topic using the source controller to add new entries editing editing the entries deleting the Angels so generally whenever we work in any model we will no more be using normal controller unless we have to but we will generally be using resource controller so that is why before proceeding with the other topics I wanted you to understand the resource controller properly and I hope this video made sense and I will continue soon with the new topic add category edit and delete using this source controller I hope you understood the video and if you did like the video don't forget to hit the like button subscribe to my channel and share the video I'll see you guys maybe today or tomorrow with a new topic until then have a nice time and if you have any confusions you can mention it in the comment and I will try my best to answer them ok then good bye [Music]
Info
Channel: Kodiary Technologies
Views: 10,747
Rating: undefined out of 5
Keywords: PHP, Laravel, Learn Laravel, Laravel from scratch, Laravel from basic, Laravel Resource Controller, Kodiary, Kodiary Technologies, Anwar Ali
Id: LatlNL9OsEg
Channel Id: undefined
Length: 22min 44sec (1364 seconds)
Published: Mon Apr 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.