Laravel 8 Blog - 5: Category Edit Update data and delete data using Eloquent in Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our larval blog series so now guys we are going to do with how to edit and update the category data in our level 8. so as now let's get started so let's move to this page at this edit button and let's create a form okay so yes let's go to our edit button and here is our edit button guys and let's create one url so url where we will tell edit category where admin forward slash edit category and here we are going to concatenate with the id so what category we are going to edit that id will pass it over here and just give a forward slash let's save and now guys let's see the output so it's id number one right so when we click on edit it should go to edit category here so now guys uh let's move back and create a route so you just need to copy this route and let's go to our web.php let me zoom it okay so first create a get method paste your route we are having that admin prefix already so we don't need to call again and category underscore id all set so here you are getting the id and then go to your controller so let's copy the code and paste here let's go to cutting controller and on the edit function so let's move to our category controller and create an edit function let's move below public function with the function name edit and we are getting the id so let's get that id over here dollar category underscore id all set so let's create one variable called category is equal to call your model category colon colon use find function where dollar your category id so here we are going to find the data and then let's return view in the page where admin slash category dot edit okay.blade.php file in that and let's pass that variable just we have created using compact function let's send the variable so let's create the file guys first let's move to resource views admin category and inside category create a new file edit dot blade dot php so here's the file and this will be linked to that file perfect so now guys we need the form to edit so what we are going to do we will take the same form of add category so let's copy this form and paste that so let's move to add category create.blade.php file let's copy all the code and get back to your edit blade and that all set so now let's change the heading edit let me just minimize this and here is to show any error comes in that form saving and here you are going to change the url update forward slash and then concatenate with the id so the variable what you have passed from your controller category let's go to this plate file and paste that category of id and we require the method post and here enc type multi-part form data we require this guys this is very much important to upload an file or any type of image okay so we are going to update this record using method put so at method we are going to use put all set so now guys uh as you as usual let's go to its input field and echo it's a record so dollar category category of name okay so your database column field name now let's let's see the output for this first record let's save and here we are having that category name is php let's edit and see the data cool so you can see that category name is coming as php guys so same way let's get for the slug let me copy and paste so here we move it slug refresh code so that php also is slug so same way guys we are going to just echo that all the values in each input field this is text area so you have to directly call that done and this is your file so we don't require to call that image if you want to show you can just show in a small box okay and now let's move with the meta title it's an input type let me just copy from here paste and all set so let me just finish and show you this guys so guys i have just finished with this so you can see that description meta keyword and then coming back to the status mode over here you can just directly paste that and check you can just add that checked option if it is that navbar status is one or this normal status is one you can tell it is checked else unchecked okay and let's update this button name update category and it should be submit so now guys let's go to top and create a route with this so before creating route let's check the output once let's refresh cool so you can see all the data has come perfect so now guys we need to just update the category so yep let's get back and copy the path with the id we have to send using put method okay so let's go to web.php and create a route we are going to update the data using put method and here is a path so we have the prefix as admin the category id let me just copy from top and then go to your controller so we will just copy it copy the controller paste here and let's go to update function so move to your controller and create a function named update so public function update function and now guys you just need to write the code so we have already written for save right so whatever validated data everything validation should be done so let's copy this full code just copy and paste for update part so here inside your update function you need to send your um the reform request so in store module we have called that so let's call this in the update function and here let's get the category underscore id okay so this category id is coming from your route all set so now with the help of this id you're going to find that category so let's edit colon colon find function and inside this dollar id so now we are going to find it and then update the record as simple guys so now uh over here you just need to change that function name from save to update and let's change this message to update it successfully okay and now guys let's update the record and see so yep over here so refresh guys and now let's try to update the record okay so welcome to php tutorial and here you can add meta description let me just add directly and here php tutorial meta keywords and now let's update okay so let's see what happens image field is required okay we need to keep nullable so while updating record we don't require the image so let's move to this validated request where inside the form category i mean category form request so let's go over there and for the image here we have kept directly required right we can keep in a level also so let's keep it nullable and once refresh now guys let's welcome to php tutorials and update this meta description meta keyword and now let's update and you see cool so category updated successfully now let's see that it's updated or not so we have changed that meta keyword and description perfect so this is also strained and here you see the data is updated so let me just show that status is hidden update category and you find that stated must be true or false okay okay so let's get back and let's move below boolean is there let's remove it because we don't require that because we have to we are accepting nullable values right so sometimes it's required or not required if you want to make mandatory you can just add that accepted accepted and you need to remove this part okay so it's your it's on your basis but i'm not going to keep mandatory it should be nullable option so now guys let's get back and now let's check and update category so yes you can see category updated successfully and here you can see that status has changed to hidden so let's unchange that okay i'm going to update perfect so it's now uh while updating this data if you replace the image so in that case what we have to do we have to delete this image and update with new image okay so guys now let us see that how to do it so let's get back to our code and go to your category controller so this is our image request part right so over here we have to add one three lines of code where we are going to check the destination dollar destination is equal to the path so the path is like uploads slash category and then let's concatenate the dollar category variable and find the image i mean the path of this image so this is the image part and now guys let's copy this and use a if condition and use file colon colon exist okay and just check that if image exit or not in this path if it is exist then please let's tell colon colon delete okay so what you are going to delete the path of this destination image so that's it guys so this is about our existing image deleting the data so if if you're using this file please import its class so just need to import this class okay and now guys let's check with the updating image also so let me just show you that in the public folder we have this image okay so here you find that php tutorials right so yep so let's open and here you find php okay so we are going to delete this and we will update this image let's open and now guys let's update the category so let's check okay category updated successfully and you find the image also is updated and let's see that old images is deleted or not perfect so that the image also is deleted so guys now i'm going to update the same image what it was so let's edit select and update with the same image update category done guys so yep guys and now let's begin with the deleting this category okay so yep let's get back to the table let's move to the index part and here table data and let's add a tag so you can just give a delete category with the id and just tell the delete button done and now let's add one extra column for delete and here change this button to danger let's save and now guys let's see the output cool so when you click on this it should delete the data so yep guys now let's copy this url with the id and then let's create a route so we are going to going to use get method to delete the record colon get method where delete category with the id category underscore id and we don't require this admin prefix because we have already done here and yep guys let's give the controller so let me just copy and paste and go to destroy so let's move to this category controller so public function name destroy and here we are going to get the id dollar category id and with this help of id you're going to search that category using your category model colon coolant find where you are going to find this if you find the id let's delete the record dollar i mean dollar category of delete function okay and after that you can just redirect this message delete it successfully else you can tell no category id found after deleting let's move to this page okay so now guys let's get back and let's add one dummy category and let's delete that category let's add so i'm just going to add some random data so here we add but select the image open and yep let's save data save category category added here asd data is added so guys when you click to delete the image also should be deleted right but we did not write the code for that over here so let's write the code for it so let's paste here so guys here let's create a variable called destination and that destination part is uploads category and we are finding that image and then we are checking that if file exists then please find that and then delete the record i mean image according to this path so while we save or update data we have got this right so same path you are going to check that delete the file and then delete the record okay so that means it will delete the image also so now let's test it once okay let me refresh and now guys i'm going to delete so let's click on delete and you find that category deleted successfully okay so let's get back and you can find that image also should be deleted so we had two images right let's go to public uploads category and only we find that only one image that is the left out this image and that image is deleted guys so guys in this video that's it we have successfully completed with our how to edit and update and then delete the category data so thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 7,515
Rating: undefined out of 5
Keywords: fundaofwebit, how to edit update data in laravel, category edit update data using eloquent in laravel 8, update data in laravel 8, how to edit update data in laravel 8, laravel 8 blog admin panel, laravel 8 admin panel blog tutorial, laravel 8 blog tutorial, blog tutorial laravel 8, laravel blog tutorial, laravel 8 blog, laravel 8 blog project, how to edit update category in laravel 8
Id: fr7yKvJ1tWM
Channel Id: undefined
Length: 18min 25sec (1105 seconds)
Published: Tue Feb 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.