Laravel 9 Ecommerce - Part 11: Product read, edit update and delete with images in laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our level 9 ecommerce series so now guys in this video we will be fetching all the products so we have completed with ad products in previous video and now we are continuing the product crud so now guys let's get started so let's move to our application and just go to that index page of your product okay so we are on the product controller at index page here is the index page and now here we have to design the table to show the products so table dot table dot table bordered dot table script table head table row table heading which is id the category we want to show the category also and the product and one product price i want to show let's tell quantity and one status the product is visible or hidden and finally an action so where you can edit and delete all set so now let's begin with table body table row and table data so before this you have to use the for each loop so instead of for each let's use for else okay and you cut this copy cut paste and paste again so what we have to do how many three plus three one seven okay so here you're going to tell call span 7 until no products available and if the data is available then you'll get all the product items so let's create the products variable and here product item so just call product copy this and paste that open and close curly brace product of id first copy and paste then product name and not product name we have to get the category right so let me type category id first and then we will see how we can call exact the name of that category and then guys you are going to call selling price and then quantity finally one status which is true means which is one we'll show it as hidden else visible visible okay save it and then finally add table data with the a tag edit class button button success for edit and for delete it's danger delete that's it so now guys so now guys let's copy this products variable and go back to your product controller so add the product controller at index function you have to send the variable and call your products model product colon colon all and then you're going to send in a compact function that is nothing but your products variable copy that paste and then you go to your products variable save and now let's refresh okay so product model not imported let's import its class here we imported now if you refresh you get the data cool so guys now you can see the product name price quantity status edit and delete button let's keep this edit and delete button small let's go ctrl d button small sm all fine so now guys uh i want this category to be displayed its name not the id so what you can do just take category category of name so how is this going to work so what you have to do the variable you are getting products so go and check in that whether that has its belongs to relationship or not so let's go to that product model product.php the model let me show you that so here product model and create one function with the name category okay where you'll be telling return dollar this belongs to this product belongs to which category okay category class and go get your product i mean category id copy the category id paste that and you go to that products of primary id okay so now let's copy this category and get back to your index blade file and now if you give this then it will work fine so refresh here you see that category name has come cool so now guys uh you need to do one exception over here let's copy this and like when you miss that anytime category so you can just add a validation part like and if that's it so if you want an else part also you can add that like no category this uh exception okay so let's save this and now you can get back refresh everything works fine so nowadays let's begin with edit and update the data so yep let's click on the edit and get the id open and close curly brace url where you'll be telling admin forward slash product forward slash your id and then edit okay so at this place you are going to take your product id let's copy the product id variable and paste here so now get back refresh looks cool now let me click on this id number three men here you see the route perfect guys so now let's create the route for this you can go to web.php and you set up that products forward slash your product id slash edit and you go to the edit function copy and move to your product controller and create a edit function so let's move to edit function function function name edit where you are getting the slug not the slug you are getting an id okay its id so the type is going to be integer and here that integer is nothing but a product now product id so you fetch the data dollar product is equal to product model colon coolant find or fail function where you will get your dollar product id so once you get the id you are going to send a return view inside your admin dot product folder dot edit blade file okay we have to create a edit blade file in the products folder let me close unwanted files which we don't require let's create a edit.blade.php all clear and now what are the form we need to design that so as we designed for product right let me show you on the add product so we have add product form right so same form we will copy and paste that okay let's get back go to your create product ctrl a ctrl c and ctrl a and ctrl v so paste it and let's change the title edit product the back button is going to be same and the list of errors we have to show if any validation is done and now guys coming to the form here you have to update the data like id so give forward slash and concatenate that dollar product of id and this is going to be a method put at method where you'll be updating using put route okay all set and now coming to your input fields so here is the first tab input field select category you are sending the variable categories and the brands also so what you have to do in your edit function you have to send your categories and edit also so now guys let me copy that [Music] so here we have the category and brand let's copy that and paste here we get the categories and brands we need to send that in a compact function compact function categories comma your brands and then your product variable let's copy this and send that as simple now you can move to your product and you get the product id and as continues so here we copy this product of id and now guys inside this select option of category let me divide this where inside this value after the value option close tag here we will directly use the variable to check the product category underscore id okay we need to check with this category id if it is double equal to both are correct means then you are going to tell selected colon null so we have to do this condition so that it selects the default and then guys you are going to again update all the input fields let's use the value attribute open and close curly brace pass the variable your name and it should be in a dollar okay so same way guys we have to update for all the fields so before updating for all the fields let's check the output guys once let me finish this brand option also let's paste here below copy the same category code we copy this and go to the options paste that and change the brand brand to brand name let's copy this variable and replace save it and now guys let's see the output so refresh okay get method is not supported let's get back to the web.php and here we are using post method right so we need to make it to get method let's refresh perfect so we are getting by default i have selected id3 and it selected the main category and it is brand is levis your shirt everything goes like that so guys let's paste for all the inputs and text areas so yep let me copy this and here you paste in your text area you don't have to give a value instead you have to directly apply that all set so guys same way you have to finish for all the fields so let me just finish and show you so this i have just pasted for the meta titles like product of meta title description and keyword and then moving with the details like original price selling price quantity and coming to the trending and status guys it's a checkbox so we have to check like this conditionally if it is a one miss then you are going to check else null so same condition for the status and then guys coming to the product images so whatever we have uploaded the images so that images should be displayed so guys for that let's uh check out with the for each loop and just design it in a image tag okay so here we check with the set function our site and call the path so before that let me loop the data so what are the data we have to get so get your product variable what you have sent from the controller and go and tell that product has many product images okay so we have already created the relationship so you need to go to your product model okay so this is my product model and here product images so let's copy this only the name and you go back to your edit blade so product has many images where you can keep this in a if condition first to check if it's available or not any one product also i mean image also so let's use else and if you can keep in h5 tag no image added or uploaded okay if image is uploaded then what then you're going to loop it so use a for each loop for each loop and you can take this product of images you're going to take as image okay and get this data and then just call that india asset function image of image let's keep inside and now you need to set a height and width so let's all set and just add one add a class let me bring it down your class is equal to so img let's end the tag and now guys let's see the output for this page also let's refresh once perfect so it's loaded and now you can move on product image so we see that guys three images have been loaded let me just add one border which will be more clear to see border save refresh go back to your product image and you see that product images are loading good so all the images whatever we have uploaded it's on the edit part so you can see all the product images so now if you require to update you can add on the image okay and about the deleting image will be seeing in further so now guys let's get back all done and now let's change the button to update and then you can get back to your form tag to check whether the url is correct or not so here we see okay all set you need to copy the path and it's a method put here you are using a multi-part form data good so let's go to web.php and you just add the route using put method your admin admin is a prefix so by default at the top of your page and then you directly use the id that is nothing with your product id okay product and then update the record so let's tell update now get back to your product controller and update the record so go to the product controller create a function function with the name update so guys we are going to take the id and the request request in the sense like all your file request whatever the input requests okay so first let's get the id that is your product id and then your request the input request okay so the input instead of input request we are going to validate all the request as we have done for storing the product so let's copy that same code and paste here because we are going to validate the same input fields okay so do not worry let's get that instead let's take it reverse like first your request and then your input id product id and now guys let's validate the data okay so first step we will create a variable called product is equal to and get the category category colon colon find or fail function we need to check whether that category is present or not in the product so you need to find this category id first so for that guys you are going to take the data validated data is equal to dollar request of validated function also so from this validated data you'll find your category underscore id how are you getting this so this is nothing but your from your edit form okay this is the category id and then you're coming here so it's instead of request you're getting this in this variable and we have done a validation in this form request so you can go to this form request and check the validation so let me just show you okay so here in the beginning category id form request validation is done after the validation you get the data and now guys we need to check for the product so what we can do is that we will product category has many products search for the where condition where id so it's searching for the product data okay product table products table dollar product id okay this product id and now you are going to check for the first record perfect so done guys and once you find it if it is true if it's found then you're going to tell come in else return and redirect back okay so let's return and redirect function [Music] with the message no such product id found okay if it is a wrong category id then it will give 404 if it is wrong product id we are going to tell no such product found and where you want to redirect it's on admin slash products okay on the index page and once the id is found you are going to product ad is found then you are going to update the record like update function okay open the array and start your input field so for that guys we have already done in the store part while saving the product let's copy that same code let's copy from here copy and paste get back to the update here we paste and instead of create let's make it to update that's it so all the things are going to be same guys same inputs same validations all done and coming back to the image part like when you update the image so it should just upload the images not edit anything for editing or removing the image we will be doing another method because here it is multiple images yep let's get back to the store code okay this is my store function and here is the image code let's copy this and the written redirect code also let's copy that and just paste that okay so if you don't understand how this image is working please watch out the previous video where we have done multiple image upload and here while updating also we are doing with the same okay so i will explain you one more time like it is checking for if image file is there then it is going to come insert and then if i have multiple files it is going to upload loop and create the file name and store into the products images table okay so this is the relationship which we have seen in the previous video of product insert data okay and now once done you are going to tell product updated successfully good so now guys let's test this save it get back once refresh so let's update the product name only from shirt to i'm going to shirt updated data okay let me update this and get back to your details from selling price 2.99 i'm going to make it 199 and quantity 10 okay and let's upload one image also let me upload with this one image and this pant image okay two files are done and now let's update here we update and we see that perfect product updated successfully and you see that shirt under men we see the short short data updated okay updated data our fields are updated and let's edit and see that image also is uploaded or not let's get back here on the product image you see the two more images are added cool so now guys uh you can get back to your home and remove this we don't require and let's update that update perfect updated now you can check out with the image again once edit here we get back all set so now guys we need to remove these images how are you going to remove this images so we will set a tag over here at this images below get back to your editor so first let's move to the edit blade file go to your image part add this image on below itself i'm going to add a tag okay and now guys when we refresh and check the output we see the image and the tags are opposite okay so instead if you want to show it below you can just use a row column wise or else you can continue like this itself so let me use the row wise guys okay so yep dot row and then dot column md two only two two columns i'm going to loop let's take it the for each loop inside row and paste here and just cut this and now let me make this a tag class d block and now you can check the output refresh so you see looks good so when you click on this remove the image should be removed so as for that what you have to do let's get back and create our route a url where admin forward slash product hyphen image and then get your image id get forward slash and then concatenate here and then use delete so here product image let's copy this variable the image okay so this product image let's concatenate that product image of id so we'll get the image id and delete that particular image okay so guys let's copy this url and get back to web.php i mean completely copy this url get back to web.php here get below route coolant colon use get method to delete this record and instead of this you are going to tell i'm gonna close product underscore image id add delete and here destroy image copy this function name and go to your post controller so yep let's move below create a function and the function name destroy image get the id of image product image id send that that's it so you got the product image id and now guys you need to call your product image model so dollar dollar product image is equal to your product image model okay let me show you that model let's go to model and here is your product image model let me copy this model name product image paste that colon colon find or fail function you find that id and pass the variable so once it's found you are going to delete the data you will delete the data okay so delete function so before deleting the function you have to delete the image also okay so when you delete this the image also should get deleted so for that guys you have to check the image first so as simple use the if condition if your product image so what product image you have to check in a file colon colon exists function your product image of image so you don't have to worry about the path why because we are already storing the path in your database itself let me show you so when we go to product images we see that uploads products and this is your product image okay so we are directly checking that if it is existing then it will come and delete the product so use delete as simple guys now it will delete the product by one by one yep guys and you need to import its class eliminate support packets file class imported once deleted guys you are going to return and redirect to products page until product image deleted that's it so now let's test it let's get back so once refresh guys okay let's move to product image and if i delete this image the image should be deleted so before deleting let's see once the image okay so the image is available over here guys okay here it is available let's delete this image okay so i'm going to click on this remove remove and it goes okay it's asking for product image not found let's import its class so we missed out this class imported let me show you that where is that product image here is the product image and the file so both class is imported guys now you can get back so yep so when you delete this i want to get back on the same url not on the main page let's remove this url guys and instead let's tell back that's it now get back here we click on delete remove i have clicked on it and you're back to the same page but we did not get the message why because we have not set that message but let's check the image is available or not here you see that the fifth image is not available okay and now let's check in our folder so you see that that image is deleted perfect so guys uh one thing which is magnetic about the showing the message right so you can go to that edit blade file and just add that message over here okay user if condition if and if session of message we have sent the message and now you are going to show that data in a h4 tag open and close calibrate session of message save it now you can let's retest so i'm going to delete this the pant image okay now delete remove here you see product image deleted and let's see the output cool that image is not available and let's see here also it's deleted nice so guys so you can just add one css class over here let me make it to h5 and just add a class alert and let's success okay margin bottom two and you can instead of pasting here you can paste above this column md12 that's it okay so now guys let's get back to the product and we are done with the edit and update of the data and image also and now finally we have to start with this delete option so guys when we delete this product it should delete its images also so now guys let's begin so let's go to this delete button let's scroll below and here is the delete button guys and we are going to do confirm and delete so let's use on click return and confirm confirm function inside this are you sure you want to delete this data okay and give a question mark perfect so let me just bring this down guys so here it's clear and now you need to add the url let me zoom it url function where we'll tell admin forward slash products forward slash your id and then delete let's give forward slash delete so here you are going to pass the product id so let's concatenate and paste your product id perfect so once you delete this you are going to this url so now guys let's copy this url and go to your web.php at product controller so here is a product controller and let's use delete so route coolant coolant get method and the route your set is the product id so let's use underscore and remove all these variables and concatenation and give product id of delete at what your destroy method let's try copy this and go to your product controller guys and create a desktop function and move to product controller let's move below guys so here now create a function function name called destroy and here you set the type as integer and you will get your product underscore id and we don't require null so once you get the id you search for the product first dollar product is equal to your product model colon colon find or fail function so now you send the product id so once you get the product id you have to delete the images first all its images and then delete this particular product so let's let's use a for each loop and check and delete for all the images so dollar product of your relationship of product images so this product has many images let's go to this model product model where we have set product images let's copy this function name copy the function name and go paste here so now as you need to check whether if n is a card exist or not for this particular so you can use an if condition if any product exists i mean images are added then you can loop that images and delete so let's use forage open and close open and close curly brace where for each of your product images as dollar images okay image set and now guys you need to check whether the file exists or not let's use if condition over here open and close curly brace where file colon colon exists function dollar image this dollar image okay and then my column field over there image okay so same concept guys we are going to copy this and paste here and just change to delete a simple semicolon and now guys once done let's use this dollar product and delete the product data delete function that's it once deleted let's return our redirect back product deleted with all its image okay okay so guys we have added this bracket so we don't require to add this bracket while we are calling any other function so that time we require bracket okay so like when we are creating or updating the record that time you use the bracket so now to read you can just loop this directly and now let's test again with the second uh data so here we have the laptop images one and two let me see the images also product image okay we have two images that is your two laptops now let's delete yes delete cool so product id deleted let's get back and here you see that laptop images are also deleted so guys we have successfully done with our products delete with the images and now guys let's continue next video with the colors and size of its product okay so whatever you want to set for each product you can set the color and size also so guys in this video that's it thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 14,029
Rating: undefined out of 5
Keywords: fundaofwebit, how to delete data from database in laravel, edit update data in laravel, laravel 9 product crud, how to make product table design for ecommerce, laravel 9 display data in table, laravel 9 ecommerce tutorial, ecommer project in laravel 9, laravel 9 ecom project, edit update data in laravel ecom, laravel 9 crash course ecom, product ecomerce tutorial, ecommerce source code
Id: PhDkujgfa4Y
Channel Id: undefined
Length: 40min 13sec (2413 seconds)
Published: Thu Jun 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.