Laravel - Day 10 - Editing and deleting entries from database using Resource controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello what's up everybody how's it going welcome to another video of tutorial series learning larvan from scratch I'm your host annually I'm here today with a new topic and the topic is using resource control in order to edit and delete entries from the database look in the last video we covered how to add a new entry to database and in order to do that we created a categories controller inside the admin folder and we modified the routes accordingly so we added a suffix called admin for the admin folder and the categories controller inside the admin folder we modify the routes a little bit so this was the modification in the resource round that we made last time and today we'll continue and work for editing and deleting entries from the database so let's begin with the index plate that we were working last time so here I need to add a route for edit and if you go to the resource controller and documentation in order to edit an entry we need to add the route name as photo dot edit but if you see in order to add and the route name called photos dot edit we also need to pass a parameter here this is a parameter that we need to pass so route has a simple rule wherever we place something like this this this is representing something like ID or maybe a slug or any parameter that we are about to pass to the entry so in this case case what happens is this this is representing an ID and in order to pass an ID to a URL or to our route there is a specific way to do that and will be also covering that specific way today so first of all let us just give a route name simply by adding two curly bracket and route and the name of route is since we have a suffix called admin we will and type in admin dot categories dot edit at the same time because need to pass a parameter as represented by this so this is representing a parameter that is to be displayed in the route so the parameter is to be passed this way so we just type in a comma and give the parameter followed by the round name so our parameter would be dollar C ID this way let's see if it works okay yes it is working now let's just try to edit hover over the links so you can see on the bottom there is categories last ones last edit categories last two slash edit categories last three is less edit right so it is working perfectly fine so this is the way how we pass the arguments of parameter or the URS segment that the URL is expecting so in this case the URL is expecting something in the middle of photos and edit and that is for our case currently it is an ID so we are passing an ID to the route name by just a comma and followed by the followed by something that we are about to pass in this case it's eye so we pass the ID to the route so now when once we click on it it it will take us to category / 1 / edit as displayed over here so photo / 1 / edit ok so this is the first thing that we covered we give we gave the link to the edit edit button in each loop now we need to work out on edit page okay so what I want you to do here is I want you to replace this ID by Nora category dollar category just like we did it in store so here and the difference here is in Stowe in the category this category was representing an empty object of a modal category in order to save that model to the database to save the values to the database but in this case since ID is being sent through the URL in this case this category will represent the specific entry from the database whose ID is the ID that is being sent through the URL so here if you just go and type in echo dollar category ID you will see that it will return the ID that is being sent through the URL in this case one similarly if you say category is less arrow title you will find that this time this will print out the title of the ID one which has a sports so you can see that how easily we can get the entry that we are about to and the entry of the ID that we passed from the URL so in case of other frameworks or in case of corporate we would have to write a query select all from categories where ID equals through the past ID but in this case just by just replacing the ID by our resource or an object of category we just fetch the entire entry of the database of the table in the database by just creating a small object as in a parameter so now what we need to do is since we need to auto populate the form using the fields using using the value of the category and what we can do is we can create an array and call it like something like category equals to this category that is we just fetched right so we are assigning this category and this category is representing a specific entry of the table and we are assigning this category to an array whose index is category I will send this area to the view just like we did in index function we created our categories where this is representing the entire entire entries of the database and we are sending this entire entry to the view file we'll be using the same way in order to send the value to the view file so here what we can do is return view and in this case our view is inside admin folder inside categories folder and my view is let's say we'll create a view called edit and we'll pass this area to the view with width function and no a our our so this is how we pass an array to the view file now inside the view file we can use a variable called category in order to present this category right ok so first of all let us create a view file inside categories folder in fine control s edit dot bleed dot PHP save it now if it's going and what what I want you to do is I want you to copy the create bleed entirely and paste it over here because the form is exactly the same we just need to auto populate the value so here instead of add category I want you to change it to edit here edit and my value here would be inside in double double curly bracket and what I can type in here is dollar category title because I want to populate the existing entry of the database to this form so my title I want to show the value the current value of the title which is in the database for ID number one or any ID that is been sent to the URL so I'm just displaying the value over here but my after I submit this function or submit this form my route would be different and then store the store is full newly created value while we create something and for update we have something like update right in order to update an existing entry you don't use tool rather we use update so my so my route should be now changed to update at the same time you can see just like in edit we need a value to be passed to the URL in even in update we need to specify which ID I want to update so I'll pass my ID here just like we did last time comma and the value that I don't need to pass which is dollar category ID so I want to specify which entry I want to edit and I'll do that I just passed the ID by separating the route name it comma and it's passing the ID so we are done now let's work on the kernel it's and work in the controller in order to save so we are submitting this to update right now we need to write some coding update in order to save this and in order to save the update I again want you to replace this ID by this category ok so just replace this ID by the category now this category will represent the category now this category will present' the entry the database for and in the the entire entry in the database and representing that ID ok so it will represent its ID its title it's let's say create a date it's updated date so everything we get by just replacing the ID by category category right now what we can do is since and this request here represents and form fields like we just worked on store so here this request is now and the form values of edit plate so we will do the exactly same code it just copied the store function and paste it in the update but this time this won't add a new entry to database rather it will update the entry because the idea is being sent for the URL so it now understands that this does not need to insert an entry database but rather it should update the entry because ID is being passed from the URL and this category is representing a specific entry in the database whose ID is being passed to the URL last time when you were working with redirect we provided our URL of the function we wanted to redirect to in our data function but it is not a good practice to provide the URL to the redirect function rather we should generally use route name and the reason for this is suppose in future we want to change our URL to from admin to administrator right in this way and in this case we have to copy this link you have to copy administrator and replace in each and every place where we have used admin like in here and here in every place we have to change replace our URL but if we had used route name instead of the URL our route name would be is the same because our suffix is admin we don't need to change this suffix because this is only meant for the route name not not for the URL so if you type in admin here and L Mistretta here admin dot categories dot let's say anything index would still represent administrator categories so if we use route name instead of the URL and in future you need to change the URL of any let's say if of any URL in that case if we are using route name we will be saving a lot of time because we don't need to modify any code in that case but if we are using URL then in that case we have to replace each and every place where we are using admin to administrator so it is a good practice to use the route name instead of the URL in any function like the function like redirect or the function like route or function like URL it is always a good practice to use route name now in order to use it out name what we need to do here is reality is a function and we can just close the function over here and call a new function called route so redirect route admin dot categories dot index so we want to read add it to the index function and the route name for the index function is admin dot categories dot index as mentioned in our laravel documentation you can see photos dot index represents and the index function right so this is the route name that we want to use similarly we can do the same over here by doing this admin tour categories dot index so it might seem that we are just making our code a little bit more let's say a little bit longer but in fact we are trying to minimize all the complexities of the future and by just adding by just using the route name instead of the URL we are saving a lot of time if we want to modify our you are ok let's give it a try so we have finished our update function now let's try to change some entries that we already have it is saying admins last category is not found because maybe we have changed to administrator so let me just change it back to admin save it now refresh this now this is the listing that we have let us just try to edit the sport edit it and maybe I want to add an S at the end let's save it now you can see the s is over there let's try to edit it and remove the s and C again and it is working so we have completed our editing of an entry now finally I also want to cover the delete so let's start working with delete in our index play index played just like we have an anchor we also have another anchor and for delete but we generally don't delete an entry using an anchor rather we'll be using a four why because in order delete we need to let's say submit and value or with we generally don't use get method or post method but we need to use delete method and we need to send that type of the method that we are trying to use through a form so in order to do that let's just forget about this anchor let it be here just below the anchor what we can do is we can create another form and give a method and the method to delete it would be and the method that we need to provide is photos or destroy with a parameter ID right so our method would be route in our case admin not categories but or destroy and we need to pass a parameter ID in this case it is CID write dollar C alright so we have provided the method the method and this is action actually so we are provided action and my method over here should be post but inside just like we did in the form of edit we give a method put so I'll just copy this exactly the same and placed it over here and replace the put by destroy because it is saying I think it's delete not destroy so if I delete so it is saying you know all delete we need to submit a form with the method delete right at the same time I also need to have an input hidden input with a my token so input time because the hidden name equals to underscore token and value should be the function CSRF underscore talkin see as our air from the score token so this is must for each and every form to make any form workable in laravel we should add this input hidden we should pass the token this way so now we are ready for with the delete form I just need to submit this somehow so what we are going to do is we just leave the form like this and it does not have anything to display it is a blank form with method delete and with our out over here I just what I just need to do is after we click on this anchor I want to submit this form so what I'll do here is I will let's say give a link or javascript:void 0 in order to prevent it from let's say refreshing the page at the same time I'll add an event called on click so whenever this anchor is clicked I want to submit this form so what I can this a is on click and let's say I want to submit the form which is inside the parent TD ok so what I can do is dollar this means this anchor dot parent means this TD dot fine and I'm finding for a form okay so this is representing this form because since our form is in loop I need to be specific which form I want to submit so I'm saying in the parent of this anchor which is TD and dot fine form which is this form and I want to submit this form using submit ok let's see now if this works so we are submitting and this form so it will take us to categories or destroy so let's go to destroy which is over here and what I can do here is I can call there our category not destroy ok so I need to call the destroy function like this the category destroy and pass the ID over here like this ok so this time I'm not replacing the ID by category category because I don't want to enter and make an entry or or it update an entry rather I want to delete an entry so I'll be using ID so category destroy ID and after I'm deleting I'm retiring into the index by copying the exact same code that we did on update so after making after deleting an entry and without relating it back to the index function so let's see we have some entries like let's say I want to delete the gossips so delete it and you can see and the gossips has go in the gossip is gone so this is how we edit or delete an entry from the database I hope you understood the video and if you have any confusion regarding the video you can mention it in the comment section and if you enjoy the video if you liked the video if you understood what I was trying to say then don't forget to hit the like button hit subscribe if you have not subscribed yet and share the video if you liked it and I'll see you guys tomorrow with the new topic and till then maybe you can just go through the video revise it a little bit and we'll see you soon with a new video till then have fun good bye [Music]
Info
Channel: Kodiary Technologies
Views: 7,586
Rating: undefined out of 5
Keywords: PHP, Laravel, Laravel from scratch, Laravel from basic, Learn Laravel, Laravel resource controller, Laravel resource controller to edit, Laravel resource controller to delete, kodiary, kodiary technologies, Anwar Ali
Id: _xBJd85QWRw
Channel Id: undefined
Length: 21min 2sec (1262 seconds)
Published: Wed May 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.