#12 Adding Product to Cart in Laravel | Laravel Ecommerce Project Tutorial From Beginner to Advance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is yamin and welcome to f tech knowledge so in the previous tutorial we show the product detail over here so in this tutorial we are going to add the product to our cart so here we'll have a add to cart option over here stop buy now and if the user click the add to guard option then it will add the specific product to their cart and on one condition there will be is that in order to add the product to the user card user needs to be logged in if the user is not logged in and and then the user click on the card option then it will send the user to login page so let's go back to our code and we'll first create our add to guard button over here so we'll go to our sublime text then we'll go to resource then views and inside view views we have home and inside home here you can see product.blade.php uh here you can see for each loop dollar product as dollar product and here you can see product details and buy now option or buy now text over here which is this one by now so what we are going to do is that we will remove this buy now option and will have a add to cut option over here and we will not use anchor tag uh anchor tag for this we will use a form for f2 card because we will also have a product quantity option over here and you know and the product quantity option will be input field and in order to use the input field we need to use form so here if i now save it then if i now just refresh here here you can see we have only product details option over here so let's go back and here after this anchor tag we are going to mention a form tag over here we'll say form and inside this form uh let's say we'll have a input field uh it will be input type it will be submit and we do not require name and will have a value over here the value it will be let's say add to curve so right now if we save it then if we now refresh here uh here you can see product details and add to cut option over here so we'll have a quantity option over here so for it uh inside this form we are going to mention another input field and in this input type it will be number then we'll say name equal to it will be quantity and after this and we'll say over here that the value of this quantity will be by default one so right now if we save it then if we now refresh here here you can see we have a input field for quantity and add to god button over here obviously we are going to change the design but here you can see that we can increase the value over here and the thing is we can also decrease the value here you can see minus 2 then minus on and 0 and when we are adding a product to the card the value cannot be less than on so here we are going to say after this value we will say here minimum min it will be on it means the product quantity can't be less than on so right now save then refresh and here if i click on this button it cannot be less than on but it can be greater than on so let's change the design a little bit so here you can see we have input field over here and input field over here i mean input type and input type so here uh we'll keep those input type inside the div so here we're going to mention a div and this div will start over here then it will end after this add to cart and inside this tip we are going to mention classical to row so we will see over here class it will be row now this is a bootstrap class so if i save it now then if i just refresh here you will see it's something like this right now we'll write two more diff over here we say here div and we'll keep this input type number i mean the quantity inside this div then we'll give another div over here and we'll keep this input type submit add to cart inside this div and right now uh we are going to mention a class inside this div we say here class it will be call md for and here we let's say here the class it will be called mp4 as well so save it now and then let's refresh here and here you can see it now looks like this now let's go and change this input type field with a little bit so here we'll go to our input type number quantity over here and here we're going to mention style and inside this style we'll say here with it will be let's say 100 px save then refresh and there you go it's down a little bit on the right side and it looks better than before so i think this design is quite nice it works for me so right now what we'll do is that if the user click on this add to cart button then it will check if the user is logged in or not if the user is not logged in then it will send the user to login page and if the user is logging then it will add the product to the cart and here you can see that currently we are not logged in so we are not a logged in user so if we now click on this add to cart button then it should send us to our login page so here uh for it obviously we need to write some code and here you can see that we write a form over here so inside this form we're going to say action then we'll say here method and after method and after method here we need to mention at the top csrf token over here as well so the method will be post and here you can see action let's say we'll give 2 second bracket and inside the second bracket we're going to mention url to file bracket to single quotation and here let's say it will be at underscore cart so let's save it and right now we'll go to our web.php and create this add underscore cut route so let's go to our route then we brought php and here let me just copy it out from here control c then ctrl v and one more thing uh i would like to mention is that we also need to send the specific product id from here and here you can see dollar products a dollar product as dollar products and in order to show that uh product title or product price here you can see we write it over here uh dollar products title then uh dollar products discount price like this so here uh after this add card we'll give you a comma over here and we say over here dollar pro ducts here you need to give a s over here because we have s over here then we give this arrow sign and we say here id so let's save it so here uh using this dollar product we are getting this that specific dollar product id and we are sending this to our web.php i mean over here so here we already copy this route and paste over here so here instead of products underscore detail we'll see over here add underscore card and obviously we'll also have this slash second bracket id and we'll use the home controller and we need to mention the function name here and let's say the function will be add underscore card i think that's fine so let's save it and we'll now go to our home controller and create this route so let's go to our app then http then controller and here you can see home controller so let's go down directly and here we're going to say public function add underscore card to first bracket and here we need to first cache the id over here dollar id to second bracket and here first of all uh we'll check that if there is any user i mean if the user click the add to cart option will check if the user is logged in or not so that is really simple so in order to check if there is any logged in user we need to write a if condition over here will say over here if then we start here auth double clone we say id then to first bracket now this will check if there is any logged in user or not and one more thing and that is if you are using or double clone id to file bracket you need to make sure that you add this use illuminate backslash support backslash packets backslash auth at the top of your controller so here uh when we are coming to this public function ad card we are first checking if there is any user logged in or not okay if the user is logged in uh if there is any logged in user let's say we just uh stay on the same page let's say return redirect to first bracket this arrow sign and we save back so the user will be stay on the same page uh if there is any logged in user but if the user is not logged in when the user click the add to guard button then we'll go to our s collision so here if the user is logged in then they will come over here and if the user is not logged in then they will come to this else condition and from this else condition will return the user to the login page so we'll say here return redirect and it will be to first bracket to single quotation and we say here login so right now let's save it now you might be wondering where we did not create this uh uh login route or view don't worry uh this login redirect was created by uh laravel by itself by default so let's save it let's refresh and here you can see currently we are not logged in so here if we click on the add to cart option it should send us to login page okay post method is not supported for this route supported method okay i think i did a mistake in the web.php uh here you can see that uh in this form uh we are sending the data to our database and we use post method so here we need to mention instead of get we have to write over here post so let's say then let's refresh again okay let's just go back let me just refresh here and here let's click on this add to god option and here you can see it written us to the login page because we are not logged in here you can see you're not logged in let's type for a different product let's click for this laptop and here you can see it's send us to login base now let's try to login let's login over here let's click login and here you can see currently we are not now as a logged in user and right now if we click on this add to god option it should keep us on the same page because we say it in the controller that we want to stay on the same page so here if we click on the add to cart option it should keep us on the same page and here you go so the basic structure is done so right now what we want uh is that if the user click on the active option then we want to add or send some data to our database so here uh in order to add the product to the card obviously we need to have a cut table in our database and we do not have a card table over here so we are going to create a card table uh so let me just open another cmd to our project directory and here we are going to write a command we will say here php rt sun make clone model and the model name will be cut and then we'll give a minus m over here so it will create a model and the cut at the same time okay i did a mistake in the model spelling it will be model like this so here uh it will create a model and the migration of card table at the same time so let's press enter and here you can see modulated successfully and migration is also created so right now if you go back to our sublime text then if we go to our models here you can see cart dot php and then if you go to our database then migration and inside migration here you can see uh we have a card table over here so here we are going to add some column so here first of all we're going to take uh say over here dollar table this arrow sign will say here string then to first bracket and inside this first bracket will say here name the name will be customer name actually so let's make it nullable and let me just copy it and let's just uh paste it let's say for six time for now one two three four five and six so after name will also get the user email then we'll get the user phone number after this we'll also get the user address in the card table so we also need the product uh product title let's say here product product underscore underscore title and we'll need product quantity let's just say over here quantity quantity and this one will be the product price now do you want any more data like product image uh do you want to store the product image okay let's also store the product image as well so here we'll say here image right over here image this image will be for product image so after image we are going to add two more column and that will be product id and user id so let me just copy this on then i'll paste it here twice and this one will be product underscore id let's say product underscore id and here we say user underscore id i think all those columns should be enough but let me just check over here in the product table if i miss anything we will not give the category i think that's fine category and description that's fine so right now let's save it ctrl s now let's go to our cmd and we right now write a command call php artisan migrate and if we now press enter it will migrate the card table in our database there you can see so right now if you go to our browser and if you now refresh here here you can see there is a new table called car and inside the cart table here you can see name email phone address product title price quantity image product id and user id so right now what we own is that if we click on the add together button then it will uh send the product data and the user data to the card table and that is actually really simple and here you so here you can see we have a quantity option over here and we are sending this quantity using a form but how we are sending the product data or product image to our controller so here you can see that in the product.blade.php we write over here input type number and this is for quantity and using input field we are only sending the quantity data and here you can see we are getting the product id is from this body loop and we are sending that specific products id to our web.php and this web.php sending this id to our home controller so using this id we can get the specific product and we are also getting the quantity using this input field so here uh in the home controller here you can see we write public function add underscore card so uh before this dollar id we are going to write over here request dollar request then we will give a comma over here we are writing this dollar request dollar request because uh we are using a form and is using this form we are sending this input type i mean the input quantity so that's why we need to write this so here uh first of all we are checking if the user is logged in if the user is logging that's fine and if the user is not logged in we are sending the user to login base so right now we just uh remove this online from here return it down redirect back so here since the user is logged in so what will directly do is that here you can see that in the card table we have the name email phone and address and those are for the user so first of all uh in this if condition let's go and get the user data fast so so in order to get the logged in user data first of all we'll declare a variable let's say dollar user equal to we'll say here or double clone will say here user and indeed we wait to first bracket so right now what will happen is that uh using this or double clone user will be able to get the logged in user data so right now if we just write over here dt to first bracket and we'll say over here dollar user then give a semiclone and if we save it now then if we refresh here then if you click on the add to cart over here you can see that something like this and here you can see attributes if i click on this here you can see that we can get the uh username email and the user type let me just zoom in over here here you can see we can get the phone number and add this everything over here so right now uh using this dollar user will be able to get the user data so we are able to get the user data so right now let's go and get the specific product data so now let me just go back and here you can see the product data over here and we are sending the product specific product id from here to our home controller so what we are going to do is that we just give over here dollar with our hair product we declare a variable let's say dollar product equal to we'll mention our product table name we say here product then we'll give double clone then we'll see over here find and we'll get to first bracket and inside it will say id so i hope uh you are clear up until now whatever i am trying to explain so here let me just explain again so here we are sending the specific product id from here to our route and our route is sending this id to our home controller and using this id we'll be able to get the specific product and how we are going to get the specific product we need to uh go to our product table and from the product table we will get the specific product title category image and the price quantity or not quantity actually will get the quantity from here that is the products total quantity actually so okay let's just start our coding so here uh we are getting the specific product data so okay let me just td this dollar product so that i can show you let's say dt we'll say dollar product let's save it so right now let's refresh here and here you can see this is our laptop and if i click over here add to cart and here you can see attributes let me just zoom in and if i click on the attributes here you can see title laptop description uh super laptop image price uh there is no discount price so yeah we can also get the specific product as well so right now how we are going to uh store all those data to our cart table you might be wondering that is actually really simple so here you can see that uh this is your card table and in order to insert the card in order to insert the data to the card table we need to make sure that we add the cart model at the top of our home controller so let me just copy this on then paste it here and here instead of uh product we are going to see over here cart so you need to write use app backslash models backslash card so let's go down and here we are going to declare a variable let's say over here dollar card you can declare any variable but it's better to declare a similar or logical variable that makes the most sense so here we'll say dollar card equal to new car so right now over here you can see that in the card table we have the username email phone and address so let's go and first get the user details over here so for it here we'll see over here dollar cart and this dollar curve is this one then we then we'll get this arrow sign then we'll say over here name equal to and here you can see this name is over here coming from over here this thing okay and after this here you can see that over here inside this dollar user variable we are storing the all user data right so here uh we'll copy this dollar user and then we'll give this arrow sign and we'll see our here name so it will get the specific logged in user name and we'll be able to store it over here inside this cart table so let me just explain again here uh it will get the specific user from the user table and it will get the that specific username and will store it in the card table name column so after name we'll have phone email and address so paste it over here on two and three time so after name let's say over here email equal to user email and this one will come from the user table and this is uh from the card table so here we say here phone equal to phone and this one will be the user address so we got the user data right now we will get the product data and the process is actually the same just like the user so let me just check the table again so here we have product underscore title price quantity uh image product id and user id okay i forgot to get the user id let's also get the user id over here as well so here we will say here user underscore id and this user underscore id will come from here here you can see user underscore id and here you can see username now this one will be id not not user underscore id because if we go to our user table and here you can see id is like this only id that's why uh we need to write over here id so the user part is done now let's do the product part so here we see dollar okay let me just paste it over here and here this this one will be product title so let's see over here product underscore title we cannot write over here user because this is for the product and over here you can see for product we declare this variable dollar product so let's copy it and let's paste it over here and here we'll give this arrow sign and we'll get the product title so we already get the specific product using the id so here if we go to our product table and here you guys we write our title over here like this title so here we'll say title product title we have the product price uh okay let me just copy it then paste it here and we'll say we'll just tightly see over here price okay let me copy it so after price we have the then we have the product id so here this one will be the image part will say here image and here we say product underscore id equal to it will be just only id so right now here this product underscore id is coming from here product underscore id product underscore id over here i uh by mistake i make the ps capital so i also have to give ps capital over here as well and here you can see product id right and here you can see product we write over here id because in the product table we have the id like this that's why we write over there id and the product is like this and here the last one is the quantity and we are we'll get the quantity from this from this input field so here uh we need to write it a little different so here we say here cut equal to cut quantity equal to it will be like this we need to write it over here dollar request this arrow sign and here if we go to our product.blade.php and here you can see that inside this uh form here you can see name equal to quantity like this so we need to copy this and we'll just paste it over here and here you might be wondering how we are storing the image uh like this we are actually not showing the image we are actually only storing the image name from the product table so here if we go to our product table here you can see product uh here you can see the image name this is the image name not the actual image okay we are only storing the image name over here so after this everything will just write over here dollar cart arrow sign will say here save and after storing the data we'll just over here return redirect to first bracket then this arrow sign will say here back so let's save it then let's just refresh here and i hope there is no error okay let's see here the product quantity it will be two and let's click okay let me check we are logged in okay we are now currently logged in so if we now click on the add to cart button here you can see it's reloaded and if we just go to our card table here you can see that we can get the username user email phone address you can see that see the product title price quantity the image name we can also get the product id which is two and user id is 20. okay let me just show you here you can see inside is 20 and email is user.gmail.com if you go to our users table here you can see user id is 20 and email is used at the right of gmail.com and if you go to a product table now you can see for sure the id is 2 and if we go to our card table here you can see that product id is 2 over here like this so let's add just another product and this time let's add this boutique for women and if you click on this add to cut button now you can see it's reloaded and if you just refresh over here here you can see there is another data and here you can see botic for women you can see the quantity this time is on and here you can see price 200 and here you can see the price is 20. now there is one more issue and that is uh here you can see if we go to our product table you can see uh some product has discount price and some port product doesn't have any discount price which you can see over here as well that some product doesn't have any discount price so if the product has a discount price we want to store that price that specific price over here whether if the product has a discount price it will store the discount price over here and if the product doesn't have a discount price it will store only the actual price so for it we need to write a if condition for the product price over here so we need to check that if the product has a discount price or not so here we will just give a if condition we'll give two first bracket and inside it okay let me just copy it like this control c then ctrl v and here we are going to write it over here like this we see over here this count underscore price and we'll give two second bracket okay let me just uh check it in the table uh let me just go to our product table and here uh discount underscore price okay that is fine so here we are checking if the product has a discount price so if the product has a discount price then let me just start this line control x control v and if the product has a discount price we'll store the discount price to the cut price now in the column of card price so here we say it like this this count underscore price and if the product doesn't have any discount price okay we need to write it a little different we need to write it like this not equal null so here we are saying not equal null it means the product has a distance price so if the product has a discount price then it will come to this if condition and if the product doesn't have any discount price then it will come to this else condition and store the product actual price to this card table so i hope i am clear but let me just explain again so that you are clear as well i want to make sure that you understand everything so here or let me just show it over here here you can see not every product has a discount price so here we are giving a if condition and we are checking uh discount price not equal now now not equal null means that this product has a discount price then we'll store that this comprised to this uh card table and if the discount price is null then we'll come to this else condition and we'll store the product price to this card table so only one of the one of the condition will happen at a time so right now let's save it and let's just refresh here and here you can see that uh this product this is a necklace and this product has a discount price which is on double six and the actual buy price is 200 so right now if i click on the add to cart the price should be a dollar on double six so let's click add to cart is reloaded and if we refresh over here at cart table and here you can see that price is on double six so right now let's add a product that uh doesn't have any discount price here you can see this product doesn't have any discount price so if i click on the add to cart and if i just refresh here and here you can see it stored 380. okay right now let's just delete those two data from here okay now there is one more issue let me just show you and here uh let's add let's add this product samsung mobile and let's say over here point it will be three and if i click on the add to cart you can see it's reloaded and if i now refresh here here you can see the quantity quantity is 3 and the price is 380. now this shouldn't happen and the reason is that if the quantity is 3 then it should multiply the price by 3 so that is actually really simple let's go back to our port and here you can see that we get that quantity over here so here you can see that we are getting the quantity over here like this dollar request quantity so here let me just copy it and here you can see that uh we have the product price over here and here over here as well this is for this one price and this is for this is for regular price so what we'll do is that will give a space then we'll give this into sign we'll give another base and we say over here request quantity then we'll give this space we'll give this into then we'll say request quantity so this is the multiple sign multiply sign so what it will do is that it will get the quantity and then it will multiply with the price so right now if we save it then if we now refresh here and here you can see that oh you can see that this is our short and its price is 17 so if i just see over here let's say four and if i click add to cart and if i now refresh here here you can see that the total price is 68 i think this part is done so right now let's go to our uh product details part and here you can see we have a add to cart option over here and if we click on the attribute option the same thing should happen so here uh what we are going to do is actually really simple we'll just go to our product.blade.php and here you can see the form over here so i'm just going to copy this form ctrl c and let's go to our product details page and these are put our underscore details dot blade dot php and over here here you can see add to cart button this is the actual button over here you can see so let me just remove this and we'll paste the form code over here which is this one and here you can see that over here we write dollar product not dollar products so here we'll remove this yes and let's just save it now and let's refresh here let's see how it looks okay it looks like this i'm not going to change the design over here i think that is fine so let's try for different product this time let's try for the toy part let's click add to cart oh not at too hard i'm sorry let's go to product details and let's see over here quantity will be pi so let's click add to cart okay everything should work fine because uh from here uh basically what is happening is that we are calling the ad card url which is in the web.php and we are also sending the id form here as well so we are basically uh sending it to this page this page over here i mean the uh this function attack so that's why we only need to paste the form over here in the product details and we don't don't have to do anything else so yeah that is all about this tutorial and if you have any question you can let me know in the comment section and you can also like mail me or you can send a message to my facebook account and i normally try to reply as soon as possible there's one more thing is that you might be wondering that after and adding the product to the card why we are not showing a message over here because uh what will do is that once we add the product to the cart will just directly send the user to the cart page so we'll in the next tutorial we'll uh create a cart page so that the user can see the product that he added to his card so this is all about this tutorial and thank you very much for watching i would actually appreciate if you subscribe to my channel so stay tuned and stay healthy
Info
Channel: Web Tech Knowledge
Views: 15,999
Rating: undefined out of 5
Keywords: add to cart in laravel, laravel shopping cart, laravel, laravel add to cart, how to use shopping cart in laravel, shopping cart in laravel, shopping cart laravel, how to make add to cart in laravel, laravel tutorial, cart laravel, laravel cart, laravel for beginners, how to make shopping cart in laravel, laravel ecommerce, ecommerce laravel, laravel e-commerce, e-commerce laravel, laravel ecommerce tutorial, laravel ecommerce project, laravel ecommerce website tutorial
Id: EaiFeRezSes
Channel Id: undefined
Length: 37min 58sec (2278 seconds)
Published: Sun May 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.