Laravel 8 E-com Part-22 : How to make add to cart in laravel using jQuery Ajax | Ajax Cart

  Рет қаралды 25,811

Sharma Coder

Sharma Coder

Күн бұрын

Пікірлер: 130
@faizudesigns1463
@faizudesigns1463 11 ай бұрын
Sir what a way of teaching, I really appreciate it!
@SharmaCoder
@SharmaCoder 11 ай бұрын
Thank you mate
@thuydungtrinh8544
@thuydungtrinh8544 Жыл бұрын
hi sir, i see you click on category name then it return to category page (at 19:04), how can I do that?
@SharmaCoder
@SharmaCoder Жыл бұрын
I have shown tht in one of my previous videos. Basically it is an tag and the href should be the category page url.
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@SharmaCoder sis how to resolve 405 error please
@yagoobali8978
@yagoobali8978 3 жыл бұрын
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
@SharmaCoder
@SharmaCoder 3 жыл бұрын
Show me your code of the route you created.
@SharmaCoder
@SharmaCoder 3 жыл бұрын
Change that to Route::post('addtocart',... )
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@SharmaCoder how to resolve this error please 405 method not allowed
@noxigen7678
@noxigen7678 Жыл бұрын
when I click add to cart, I get error 405 (Method Not Allowed), can you help please? @Sharma Coder
@najsw
@najsw Жыл бұрын
di you get the solution ?
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@najsw di you get the solution ?
@Eliptik_
@Eliptik_ 2 жыл бұрын
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
@SharmaCoder
@SharmaCoder 2 жыл бұрын
404 error is when the url you have given in the ajax is not found in the web.php Just check with that once..
@Eliptik_
@Eliptik_ 2 жыл бұрын
@@SharmaCoder I already fixed, removing the slash in the ajax code. But now I have "405" error becuase the method POST is not supported.
@youssradebbali3135
@youssradebbali3135 2 жыл бұрын
@@Eliptik_ solution please?
@najsw
@najsw Жыл бұрын
@@Eliptik_ do you get solution for this ?
@rotimiidowu6848
@rotimiidowu6848 2 жыл бұрын
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.
@mohamedlaaroussi1543
@mohamedlaaroussi1543 2 жыл бұрын
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.
@cloudeweb
@cloudeweb 2 жыл бұрын
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!
@SharmaCoder
@SharmaCoder 2 жыл бұрын
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.
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
​@@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
@usmanarif9191
@usmanarif9191 2 жыл бұрын
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?
@SharmaCoder
@SharmaCoder 2 жыл бұрын
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.
@usmanarif9191
@usmanarif9191 2 жыл бұрын
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?
@balddog470
@balddog470 2 жыл бұрын
@@usmanarif9191 i think it depends on your preference. In my country, the biggest ecommerce website uses Sharma Coder's approach.
@voskilltheentertainer2564
@voskilltheentertainer2564 2 жыл бұрын
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
@kakaisaac
@kakaisaac Жыл бұрын
Hi Good job what if i want to add to cart before logging in and then after log in that cart still exists
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
show me your code please
@thetiagothebest
@thetiagothebest 2 жыл бұрын
Hey sir, when i press add to cart I have error 405 (Method Not Allowed)
@SharmaCoder
@SharmaCoder 2 жыл бұрын
What method have you created the route? Get or Post?
@thetiagothebest
@thetiagothebest 2 жыл бұрын
@@SharmaCoder This is my route Sir, Route::post('addtocart',[CartController::class, 'addProduct']);
@SharmaCoder
@SharmaCoder 2 жыл бұрын
Show me your Ajax code.
@thetiagothebest
@thetiagothebest 2 жыл бұрын
@@SharmaCoder I have already solved it by putting the url in the ajax request as follows: "{{url('/addtocart')}}". Thanks for your attention.
@littleerror6501
@littleerror6501 2 жыл бұрын
@@thetiagothebest bro can you share the code here
@ahmedabdallah8781
@ahmedabdallah8781 2 жыл бұрын
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
@SharmaCoder
@SharmaCoder 2 жыл бұрын
In the ajax, make it as method: "post"
@ahmedabdallah8781
@ahmedabdallah8781 2 жыл бұрын
@@SharmaCoder same as in the web.php when it comes to route thx sir
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@ahmedabdallah8781 how to resolve this error please 405 method not allowed
@ismaelseck
@ismaelseck 3 жыл бұрын
hi sir i need your help jquery doesn't works and when i click on add to cart button i ain't got reaction
@SharmaCoder
@SharmaCoder 3 жыл бұрын
Place the jQuery script on top and try. Also check the console for errors.
@khadija52
@khadija52 3 жыл бұрын
Is it working now?
@ismaelseck
@ismaelseck 2 жыл бұрын
@@khadija52 yeah that’s okay now
@ismaelseck
@ismaelseck 2 жыл бұрын
@@SharmaCoder thanks you very much 🙏🏾
@youssradebbali3135
@youssradebbali3135 2 жыл бұрын
@@ismaelseck code source please ?
@kyawswarthein4032
@kyawswarthein4032 3 жыл бұрын
sir i have am working the same style flow to add qty in the cart but it won't work
@SharmaCoder
@SharmaCoder 3 жыл бұрын
Check if you have added the jquery in the top of all the other scripts.
@kyawswarthein4032
@kyawswarthein4032 3 жыл бұрын
@@SharmaCoder I have already put it on the top but it won't work sir
@anselmus.r
@anselmus.r 2 жыл бұрын
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`)"
@donaldmjbart-williams3144
@donaldmjbart-williams3144 3 жыл бұрын
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); } }); });
@trixolian
@trixolian 2 жыл бұрын
swal
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@trixolian show me your code please
@chandeletdoris6234
@chandeletdoris6234 2 жыл бұрын
Thanks sir for your tutorials,
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@aartimaurya7855
@aartimaurya7855 Жыл бұрын
Sir my url is not right in ajex
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
how to resolve this error please 405 method not allowed
@ReemRamzi
@ReemRamzi 2 жыл бұрын
You're the best instructor, thank you.
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@renzj5322
@renzj5322 3 жыл бұрын
Sir, how about using keyup?
@ngocthienpham2197
@ngocthienpham2197 2 жыл бұрын
why when i press the add to cart button it just goes in "Login to Continue"
@SharmaCoder
@SharmaCoder 2 жыл бұрын
Have you logged in before adding to cart?
@ngocthienpham2197
@ngocthienpham2197 2 жыл бұрын
@@SharmaCoder no I haven't done that function yet
@ngocthienpham2197
@ngocthienpham2197 2 жыл бұрын
@@SharmaCoder I don't need to login, what should I do?
@SharmaCoder
@SharmaCoder 2 жыл бұрын
Then directly add it to your cart.
@ngocthienpham2197
@ngocthienpham2197 2 жыл бұрын
@@SharmaCoder Where do I need to fix to be able to do so
@mohamedali8708
@mohamedali8708 3 жыл бұрын
I have 401 (Unauthorized) error , can you help me please
@SharmaCoder
@SharmaCoder 3 жыл бұрын
You might have created to add-to-cart Route in the "Auth" middleware.. Just put it outside the "auth" middleware and that should work.
@mohamedali8708
@mohamedali8708 3 жыл бұрын
@@SharmaCoder thanks a lot its working fine now
@SharmaCoder
@SharmaCoder 3 жыл бұрын
You're welcome.
@m_artistry7696
@m_artistry7696 2 жыл бұрын
@@SharmaCoder I have same error I did it but it's not working kindly help me please
@youssradebbali3135
@youssradebbali3135 2 жыл бұрын
@@mohamedali8708 solution please???
@Adilkhan-lh4tq
@Adilkhan-lh4tq 2 жыл бұрын
best sir .thank you
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@BucurIonNiculae
@BucurIonNiculae 2 жыл бұрын
This series of tutorials is amazing. You learn so much from it.
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@ismaelseck
@ismaelseck 2 жыл бұрын
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
@SharmaCoder
@SharmaCoder 2 жыл бұрын
Clear your browser cache or try to open it in incognito mode. It works.
@ismaelseck
@ismaelseck 2 жыл бұрын
@@SharmaCoder I’m not in local mode, I’ve hosted the project
@SharmaCoder
@SharmaCoder 2 жыл бұрын
I meant the mobile's browser.
@nandopookey9195
@nandopookey9195 Жыл бұрын
Thank you so much sir
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@kyawswarthein4032
@kyawswarthein4032 3 жыл бұрын
sir it show me internal server error (500)
@ahmedabdallah8781
@ahmedabdallah8781 2 жыл бұрын
did you fix this problem i am having the same problem !!
@shantanusharma2714
@shantanusharma2714 2 жыл бұрын
have you solved the problem?
@robertsonanghag6133
@robertsonanghag6133 Жыл бұрын
i have the same problem sir did you fix this problem sir?
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@robertsonanghag6133 how to fix this problem
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
​@@robertsonanghag6133 make this in CartController if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){
@tuanphamvan655
@tuanphamvan655 2 жыл бұрын
very good video this is what i was looking for thank you!
@SharmaCoder
@SharmaCoder 2 жыл бұрын
Glad it helped..
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
your code please
@donaldmjbart-williams3144
@donaldmjbart-williams3144 3 жыл бұрын
This is the route Route::middleware(['auth'])->group(function(){ Route::post('add-to-cart',[CartController::class, 'addProduct']); });
@SharmaCoder
@SharmaCoder 3 жыл бұрын
Keep the add to cart Route outside the auth middleware..
@shiromaroo2398
@shiromaroo2398 2 жыл бұрын
Why user_id, prod_id ... is string
@Code7s
@Code7s 3 жыл бұрын
may force with you
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
code source please
@nguyen_hieu_8463
@nguyen_hieu_8463 Жыл бұрын
401 (Unauthorized) error , can you help me please!!! @Sharma Coder
@SharmaCoder
@SharmaCoder Жыл бұрын
Put the add-to-cart route outside the auth middleware in the web.php.
@nguyen_hieu_8463
@nguyen_hieu_8463 Жыл бұрын
@@SharmaCoder thank you
@najsw
@najsw Жыл бұрын
@@SharmaCoder i dont understand sir, how to put outside of the auth?
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@nguyen_hieu_8463 show me your code please
@toddmcmannus4031
@toddmcmannus4031 Жыл бұрын
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']);
@najsw
@najsw Жыл бұрын
do you get the solution ?
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
@@najsw make this in CartController if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){
@IssOu-hw3eh
@IssOu-hw3eh Жыл бұрын
make this in CartController if(Cart::where('prod_id',$product_id)->where('user_id',Auth::user()->id)->exists()){
@aartimaurya7855
@aartimaurya7855 Жыл бұрын
Localhost/add_to_cart but my right url localhost/example-app/add_to_cart
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
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
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
@Sharma Coder
@SharmaCoder
@SharmaCoder 2 жыл бұрын
You misspelled check as cheak.
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
@@SharmaCoder tysm❤
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
@@SharmaCoder why response->json not work with me?
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
@@SharmaCoder can i change it to session?
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
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
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
@Sharma Coder
@rashedbahnasi2016
@rashedbahnasi2016 2 жыл бұрын
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))"
@ngocthienpham2197
@ngocthienpham2197 2 жыл бұрын
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"]); }
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
Become a PRO at Using Components in Laravel
32:51
Code With Dary
Рет қаралды 40 М.
Please stop using px for font-size.
15:18
Coder Coder
Рет қаралды 247 М.
Небанальность зла в деле Беркович и Петрийчук
22:19
НО.Медиа из России
Рет қаралды 41 М.
⚡️NEWS | UNITED RUSSIA OFFICE AND ATMS BURNED DOWN | COMPLAINING TO PUTIN ABOUT GREF
9:14
Stripe Complete Checkout Process in Laravel
1:03:36
The Codeholic
Рет қаралды 55 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 493 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН