How to Add Product to Cart in Laravel | Laravel E-Ccommerce Project Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Yamin and welcome to larel e-commerce project tutorial for beginner so in this tutorial we're going to add the product for the card so we'll let the user add the produ through their card so for it we're going to have a add to card button and we're going to create a card table in our database and in the card table we'll use user ID and product ID as a foreign key so for it key let's go back to our p and we'll put our product. blade PHP and here you can see the details Anor tag so after this Anor tag we are going to mention another Anor tag and here we're going to send add to card and let's mention it boep class here let's here glass it will be BTN BTN and let's save primary then save it and if we now refresh here there you can see we have a add to Buton right now so let's go back right now what we do is we are going to create a table for end to card so we are going to create a card table so for it we go to our forf location and could just open a command front here we say CMD press enter and here we are going to write this command PHP artisan make clone model then we'll mention the card then minus n so it will create a model and migration for guard table at the same time so right now you press enter there you can see mod and migration is created so right now if we just go back to our code then if we go to our database then migrations here you can see we have card table and if you go to app then models and here can card. PHP so first of all here we're going to create a for in key so for it we are going to say dollar table and in order to create foreign key we need to say here unide big integer then to first bracket to S we'll use user underscore ID as a foreign key after that let me just copy this and we'll paste it here and here we're going to say un sign this will be product ID so let's say here product ID and after that we need to mention here this foreign key so for it we need to dollar table this Arrow sign foreign to single user ID uh we are mentioning that we are going to use user underscore ID as a foreign key and the reference we need to mention a reference AR references and it will be ID from the user table so we need to say here on users then this Arrow sign we say here on delete it will be cascet on delete cascet and we need to keep this inside single quotation so here on delete cascet wi if you let's say delete a parent data then it will also delete the child data because it will not make any sense to keep the child data so after this let me just copy then paste it here and this time we're going to say here product product ID and our table name will be products product and it will be on update on update and let's save this and right now just go back to the common promp and listen here PHP or t migrate and press enter and there you can see all table is migrated and make sure that you have added unsign big in deser for foreign key okay so right now if we just go back to database then refresh here there you can see we have a card table and in the card table we have user ID and product em so op is that whenever someone wants to add product to their card that user must be logged in so here we are already logged in so let me just log out from here and here you can see cly we are logged out and first of all what we want is that when we click on this add to card button if the user is loging okay then it will add the product to card otherwise it will send the user to the login page so for it let's go back to the port and we go. product. great. PHP and first of all in this a we'll give to Second bracket then we send here Ur to first bracket to single quotation let's say here addcore carard and first of all in order to add a product to the card we need to get the product ID so in order to product ID this is really simple we just need to use this dollar product so we are going to say here comma dollar product this Arrow sign ID so right now let me just show you if I now save refresh here click on add to C there you can see in the URL we want to see the product ID so let's go back and let's go and create the route for this so we'll go to our web. phsp then let me just uh copy this on crl C then contrl p and here what we going to do is we're going to add middle for so let me just popy it from here form here let me just copy this and I'm going to paste it over here we see here middle all okay verified and here it will be add underscore card and we also need to get the product ID as well so here we're will say addore guard ID we use the controll function name will be addore C and here M all it means it will check whether a user is logged in or not so right now if a user is not logged in and click on the add to C Bal first of all it will check whether that user is WR Lo in if the user is logged in okay and the product C and if the user is not logged in then our project will send the user to the login page let me just show you if I just refresh here then click on add to card there you can see it send us to the login page so let's go back to the code and here right now we'll go to our home controller and create this at cut function so let's go to our home controller and here we're going to send public function atore first bracket first all we get that ID then do second bracket so in the card table we need to get the loged in user ID and the product ID okay so we are able to easily get the product ID whenever we click on Ard so right now here uh first of all let's get the product ID so we say here dollar productor ID equal to it is just uh this ID we can just say it over here ID so right now in order to add the login user ID first of all we need to add the user model at the top so let me just copy this then paste it over here and here we're going to say use app back slm slash user and after this we need to add one more thing and that is use illuminate back slash support B slash faget back slash it will be all so since we mention o and the user model so we able to get the login user ID so in order to get the loged in user ID we need to say here uh let's just declare variable let's say dollar user equal to or dou we are going to mention the user table name then to pet like this and after it let's declare another variable we say here dollar user uncore ID ort book right now we're going to mention this dollar user here and then this Aros side ID so here what we are doing is that we are getting the loing user all the data and storing it in this user variable and after that we are getting the user ID using this dollar user variable and storing the user ID in this variable so right now we have the product ID and loog in user ID so what we can do is we can just uh declare a variable or to store the product ID and user ID in the card table and we also need to add the card model name at the top as well so we mention the C model name here so we send here card we was m/ model back slash guard so let's go down here let's declare variable let's say dollar data equal to We Men the card model now and before need we need to say here new so dollar data equal to the new card then we say here dollar data this Arrow sign user uncore ID and this user uncore ID is coming from the database T here you can see user _ ID so we'll set here for ID called to this one dollar user ID and after it we're going to say let me just copy this then paste it here and here we save product underscore ID so we save product underscore ID and this product UND we come from the database table and here we're going to say this dollar foru ID and and after it we are just going to save it so we send here dollar data this Arrow side s and after saving that we'll keep the insert on the same P so we say here return redirect to F again this AR sign back so let's save this and let's go back letes and let me just log in okay let me just show you if I now click on add to card it will take to the login base it will just login then we'll just login from here click on login there you can see currently we are logged in and we have the log out option let me just go down and let's click on add to par is reloaded and it took us back to the same place and if we now just refresh our there you guys see we have a product over here let me just let me just add one more product let's choose uh this one click on ADD and if I now refresh there you can see there is one more product so right now after adding the product show a message at the top that product added for the card successfully so what it what we can do is we can use toster just like before so let me just go to the uh admin controller we'll go to app HTTP controller and here you can see admin controller and here you can see to start time out 10 second so let me just copy this and after saving the data prod card we're going to say here tost start time out close button add success uh product added to the C successfully let's say product added to the car success qu so let me just refresh here again let's click on add to Bar there you can see you are able to see M product added product card successfully and let me just refresh here there can see user ID is on each time and the product ID is different product ID is different over here so let me just log out from here and let's try to and let's try to log in as different user let's say here email will be Gamin and password then login so right now if you go down and click on add to card and click on add to card the foru added and if we now refresh here there you get see product added our there so right now let me just show you one thing let me just log out and let me just log in as user again and click on login and here you can see uh it didn't show us any message over here but if I now just log out and let's click on uh this coduct and to card and if we now login as a user and then click on login it will show us a mage spct added to the card because we logged in form using this Buttle so it will store the user ID so when you logged in it will auto ially and this certain product so here if we refresh you will see one more photo over here so this is all for this tutorial so in the next tutorial what you're going to do is that when the user added Phu to the card it will show the number of Phu that is added to the card over here and user will also be able to see all the prodct that they added to their card so thank you very much for watching make sure to subscribe and if you have any question or suggestion just let me know in the comment section and I hope will see you in the Fe everybody else
Info
Channel: Web Tech Knowledge
Views: 583
Rating: undefined out of 5
Keywords: add to cart in laravel, laravel add to cart, how to make add to cart in laravel, how to use shopping cart in laravel, laravel, add to cart laravel, shopping cart in laravel, ecommerce project in laravel 11, laravel tutorial, how to make shopping cart in laravel, how to create shopping cart in laravel 11, laravel project, laravel shopping cart, multi-vendor ecommerce in laravel 11, cart laravel, how to add product to cart using ajax in laravel, laravel 11, laravel development
Id: qJLAe8KtBR4
Channel Id: undefined
Length: 14min 30sec (870 seconds)
Published: Fri May 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.