Laravel 8 E-com Part-22 : How to make add to cart in laravel using jQuery Ajax | Ajax Cart
Вставка
- Опубліковано 10 лис 2024
- In this video, you will learn how to make the add to cart functionality in laravel 8 ecommerce using jQuery Ajax.
Donate and get Source Code : www.fundacodes...
Part-21: How to make product quantity increment or decrement : • Laravel 8 E-com Part-2...
Laravel 8 Ecom Playlist link : • Laravel 8 E-commerce P...
If the video helps you, hit the like button and subscribe to the channel. If you have any doubts or suggestions, drop them in the comments below and i will answer them.
Thanks for watching
Follow me on instagram : / sharma_coder
Sir what a way of teaching, I really appreciate it!
Thank you mate
good job but just one think if you put the route(/add_to_card)in side the middleware the allert (login to continue is dosent work because the access to this route we need to login.
You're the best instructor, thank you.
your code please
Good tutorial! Just a doubt... usually is not need to login for add to cart. Is which way should be made it, considering that user relation is not exists? I Could create an cookie with a random token and save in a column for example 'user_id_temp' for not logged user and after logged assign user_id? thanks!
We can do it that way. But if the user visits the website after a month, (assuming he cleared browser cookies and cache) he wont find the product in his cart.
Also if the user doesn't visit the website using the same device, he will again not be able to view his cart items.
So by storing the cart items in database, the user is more likely to purchase the product. Even if he comes back after months, his cart items will still be there.
You can still build it that way, but i prefer storing it in the database with the user_id.
@@SharmaCoder hello mr sharma i got thid error in consol:
Error handling response: TypeError: Cannot read properties of undefined (reading 'direction')
at l (chrome-extension://aggiiclaiamajehmlfpkjmlbadmkledi/contentscript.js:26:89552)
at chrome-extension://aggiiclaiamajehmlfpkjmlbadmkledi/contentscript.js:35:36
Thanks sir for your tutorials,
your code please
This series of tutorials is amazing. You learn so much from it.
your code please
best sir .thank you
your code please
Thank you so much sir
your code please
Hello, I am confused that why you are not using sessions for cart functionality? Is it good for performance point of view to make entry in cart table?
You won't get your cart data if you login from other devices. Once session expires, even the same device will not display the cart items.
So you lose sales if you don't store it in db.
If you have storage issues, clear the cart after 30 days of inactivity.
Thanks for reply But still have confusion. if there are multiple users which are adding products in cart, on every entry in cart table wouldn't become reason load on server? For multiple devices your work is great but I just want to know that is it standard way or is there any other way too for this cart module?
@@usmanarif9191 i think it depends on your preference. In my country, the biggest ecommerce website uses Sharma Coder's approach.
please what could be the cause of this error on this part 22 am getting the below error please help me
The GET method is not supported for this route. Supported methods: POST.
very good video this is what i was looking for thank you!
Glad it helped..
your code please
Sir, how about using keyup?
Hi Good job what if i want to add to cart before logging in and then after log in that cart still exists
show me your code please
hi sharma i am having a bug whereby the product_id remain the same in all products i have followed every bit of the tutorial.kindly help
Hi sir,
I faced a problem.
When I add to cart, it tell me the post method is not support it for this route
why I applied every single thing after you.
The problem is
the post method is not support for this route
please please help me
Show me your code of the route you created.
Change that to Route::post('addtocart',... )
@@SharmaCoder how to resolve this error please 405 method not allowed
when I click add to cart, I get error 405 (Method Not Allowed), can you help please? @Sharma Coder
di you get the solution ?
@@najsw di you get the solution ?
Hey sir, when i press add to cart I have error 404, the route in web.php and in the ajax are the same (in ajax I have the /) add-to-cart
404 error is when the url you have given in the ajax is not found in the web.php
Just check with that once..
@@SharmaCoder I already fixed, removing the slash in the ajax code. But now I have "405" error becuase the method POST is not supported.
@@Eliptik_ solution please?
@@Eliptik_ do you get solution for this ?
Hello Sharma I’ve followed your tuto until the end and I realise that increment and decrement buttons don’t work on mobile devices. They only work on PC
Clear your browser cache or try to open it in incognito mode. It works.
@@SharmaCoder I’m not in local mode, I’ve hosted the project
I meant the mobile's browser.
This is the route
Route::middleware(['auth'])->group(function(){
Route::post('add-to-cart',[CartController::class, 'addProduct']);
});
Keep the add to cart Route outside the auth middleware..
hi sir, i see you click on category name then it return to category page (at 19:04), how can I do that?
I have shown tht in one of my previous videos. Basically it is an tag and the href should be the category page url.
your code please
@@SharmaCoder sis how to resolve 405 error please
mr sharma if i may i am following your steps and it gives me The GET method is not supported
and i copied the code for the $ajax
In the ajax, make it as method: "post"
@@SharmaCoder same as in the web.php when it comes to route thx sir
@@ahmedabdallah8781 how to resolve this error please 405 method not allowed
how i fix this? SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prod_id' in 'where clause' (SQL: select exists(select * from `carts` where `prod_id` = 4 and `user_id` = 1) as `exists`)"
Hey sir,
when i press add to cart I have error 405 (Method Not Allowed)
What method have you created the route? Get or Post?
@@SharmaCoder This is my route Sir,
Route::post('addtocart',[CartController::class, 'addProduct']);
Show me your Ajax code.
@@SharmaCoder I have already solved it by putting the url in the ajax request as follows: "{{url('/addtocart')}}".
Thanks for your attention.
@@thetiagothebest bro can you share the code here
Sir my url is not right in ajex
how to resolve this error please 405 method not allowed
sir i have am working the same style flow to add qty in the cart but it won't work
Check if you have added the jquery in the top of all the other scripts.
@@SharmaCoder I have already put it on the top but it won't work sir
hi sir i need your help
jquery doesn't works
and when i click on add to cart button i ain't got reaction
Place the jQuery script on top and try.
Also check the console for errors.
Is it working now?
@@khadija52 yeah that’s okay now
@@SharmaCoder thanks you very much 🙏🏾
@@ismaelseck code source please ?
The alert message is not working.
$.ajax({
method: "POST",
url: "/add-to-cart",
data: {
'product_id': product_id,
'product_qty': product_qty,
},
success: function (response) {
alert(response.status);
}
});
});
swal
@@trixolian show me your code please
why when i press the add to cart button it just goes in "Login to Continue"
Have you logged in before adding to cart?
@@SharmaCoder
no I haven't done that function yet
@@SharmaCoder I don't need to login, what should I do?
Then directly add it to your cart.
@@SharmaCoder Where do I need to fix to be able to do so
Why user_id, prod_id ... is string
I have 401 (Unauthorized) error , can you help me please
You might have created to add-to-cart Route in the "Auth" middleware..
Just put it outside the "auth" middleware and that should work.
@@SharmaCoder thanks a lot its working fine now
You're welcome.
@@SharmaCoder I have same error I did it but it's not working kindly help me please
@@mohamedali8708 solution please???
sir it show me internal server error (500)
did you fix this problem i am having the same problem !!
have you solved the problem?
i have the same problem sir did you fix this problem sir?
@@robertsonanghag6133 how to fix this problem
@@robertsonanghag6133 make this in CartController
if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){
may force with you
code source please
sry mr.sharma but i have another problem
exception: "Illuminate\\Database\\QueryException"
file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php"
line: 760
@Sharma Coder
message:
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'carts.products_qty_foreign' (SQL: insert into `carts` (`product_id`, `user_id`, `product_qty`, `updated_at`, `created_at`) values (16, 1, 1, 2022-12-03 15:24:59, 2022-12-03 15:24:59))"
401 (Unauthorized) error , can you help me please!!! @Sharma Coder
Put the add-to-cart route outside the auth middleware in the web.php.
@@SharmaCoder thank you
@@SharmaCoder i dont understand sir, how to put outside of the auth?
@@nguyen_hieu_8463 show me your code please
public function addProduct(Request $request)
{
$product_id = $request->input('product_id');
$product_qty = $request->input('product_qty');
if(Auth::check())
{
$prod_check = Product::where('id',$request->$product_id)->first();
if($prod_check)
{
if(Cart::where('prod_id',$product_id)->where('user_id',Auth::id())->exists())
{
return response()->json(['status'=> $prod_check->name."Arly Add to cart"]);
}
else
{
$cartItem = new Cart();
$cartItem->prod_id = $product_id;
$cartItem->user_id = Auth::id();
$cartItem->prod_qty = $product_qty;
$cartItem->save();
return response()->json(['status'=> $prod_check->name."Add to cart"]);
}
}
}
else{
return response()->json(['status' => "Login to Continue"]);
}
i have this problem
message: "Method Illuminate\Auth\SessionGuard::cheak does not exist.",…}
exception: "BadMethodCallException"
file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php"
line: 113
@Sharma Coder
You misspelled check as cheak.
@@SharmaCoder tysm❤
@@SharmaCoder why response->json not work with me?
@@SharmaCoder can i change it to session?
Localhost/add_to_cart but my right url localhost/example-app/add_to_cart
I am struggling with this part. POST localhost:8000/add-to-cart 500 (Internal Server Error) This is my error. Route::post('/add-to-cart',[CartController::class,'addProduct']);
do you get the solution ?
@@najsw make this in CartController
if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){
make this in CartController
if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){