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

КОМЕНТАРІ • 130

  • @faizudesigns1463
    @faizudesigns1463 10 місяців тому +2

    Sir what a way of teaching, I really appreciate it!

  • @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.

  • @ReemRamzi
    @ReemRamzi 2 роки тому

    You're the best instructor, thank you.

  • @cloudeweb
    @cloudeweb 2 роки тому +1

    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

  • @chandeletdoris6234
    @chandeletdoris6234 2 роки тому

    Thanks sir for your tutorials,

  • @BucurIonNiculae
    @BucurIonNiculae 2 роки тому

    This series of tutorials is amazing. You learn so much from it.

  • @Adilkhan-lh4tq
    @Adilkhan-lh4tq 2 роки тому

    best sir .thank you

  • @nandopookey9195
    @nandopookey9195 Рік тому

    Thank you so much sir

  • @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 роки тому +1

      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 Рік тому

      @@usmanarif9191 i think it depends on your preference. In my country, the biggest ecommerce website uses Sharma Coder's approach.

  • @rotimiidowu6848
    @rotimiidowu6848 Рік тому

    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.

  • @tuanphamvan655
    @tuanphamvan655 2 роки тому

    very good video this is what i was looking for thank you!

  • @renzj5322
    @renzj5322 3 роки тому

    Sir, how about using keyup?

  • @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

  • @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

  • @yagoobali8978
    @yagoobali8978 2 роки тому +1

    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  2 роки тому

      Show me your code of the route you created.

    • @SharmaCoder
      @SharmaCoder  2 роки тому

      Change that to Route::post('addtocart',... )

    • @IssOu-hw3eh
      @IssOu-hw3eh Рік тому

      @@SharmaCoder how to resolve this error please 405 method not allowed

  • @noxigen7678
    @noxigen7678 Рік тому +1

    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 роки тому +1

    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 роки тому +1

      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 роки тому +1

      @@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 ?

  • @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.

  • @donaldmjbart-williams3144
    @donaldmjbart-williams3144 2 роки тому

    This is the route
    Route::middleware(['auth'])->group(function(){
    Route::post('add-to-cart',[CartController::class, 'addProduct']);
    });

    • @SharmaCoder
      @SharmaCoder  2 роки тому +1

      Keep the add to cart Route outside the auth middleware..

  • @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

  • @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

  • @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`)"

  • @thetiagothebest
    @thetiagothebest 2 роки тому +1

    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

  • @aartimaurya7855
    @aartimaurya7855 Рік тому

    Sir my url is not right in ajex

    • @IssOu-hw3eh
      @IssOu-hw3eh Рік тому

      how to resolve this error please 405 method not allowed

  • @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

  • @ismaelseck
    @ismaelseck 2 роки тому

    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  2 роки тому +1

      Place the jQuery script on top and try.
      Also check the console for errors.

    • @khadija52
      @khadija52 2 роки тому

      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 ?

  • @donaldmjbart-williams3144
    @donaldmjbart-williams3144 2 роки тому +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);
    }
    });
    });

  • @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

  • @shiromaroo2398
    @shiromaroo2398 2 роки тому

    Why user_id, prod_id ... is string

  • @mohamedali8708
    @mohamedali8708 3 роки тому

    I have 401 (Unauthorized) error , can you help me please

    • @SharmaCoder
      @SharmaCoder  3 роки тому +1

      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???

  • @kyawswarthein4032
    @kyawswarthein4032 3 роки тому

    sir it show me internal server error (500)

    • @ahmedabdallah8781
      @ahmedabdallah8781 2 роки тому +1

      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()){

  • @Code7s
    @Code7s 3 роки тому

    may force with you

  • @rashedbahnasi2016
    @rashedbahnasi2016 Рік тому

    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 Рік тому

      @Sharma Coder

    • @rashedbahnasi2016
      @rashedbahnasi2016 Рік тому

      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))"

  • @nguyen_hieu_8463
    @nguyen_hieu_8463 Рік тому

    401 (Unauthorized) error , can you help me please!!! @Sharma Coder

    • @SharmaCoder
      @SharmaCoder  Рік тому +2

      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

  • @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"]);
    }

  • @rashedbahnasi2016
    @rashedbahnasi2016 Рік тому

    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

  • @aartimaurya7855
    @aartimaurya7855 Рік тому

    Localhost/add_to_cart but my right url localhost/example-app/add_to_cart

  • @toddmcmannus4031
    @toddmcmannus4031 Рік тому +1

    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()){