1

I have build a shopping cart..Where I tried to update database using ajax..when I will update the quantity, it will reflect the database immediately..

for that i used the following View:

 @foreach($carts as $row)
 <input type="hidden" class="cat_id" value="{{$row->id}}"/>
<input type="number" class="quantity" value="{{$row->no_of_items}}" name="qty" maxlength="3" max="999" min="1" /> &times;${{$row->p_price}}
 @endforeach

Here is the Ajax Part:

$(".quantity").change(updateCart);

    function updateCart(){
        var qty = parseInt($(this).val());
        var cat_id = parseInt($('.cat_id').val());
        console.log(cat_id);

        $.ajaxSetup({
                                headers: {
                              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            }
                             });

          $.ajax({  
                            url:"{{url('cart/update/{cat_id}/{qty}')}}",  
                            method:"POST",  
                            data:{cat_id:cat_id, qty:qty},                              
                               success: function( data ) {
                         // console.log(data);
                        }
                       });
    } 

Route

Route::post('cart/update/{cat_id}/{qty}','ProductController@cartUpdate');

And the controller part :

public function cartUpdate($cat_id, $qty)
    {
        $cart = Cart::find($cat_id);
        $product = Product::find($cart->product_id);
        $cart->no_of_items = Input::get('qty'); 
        $cart->price = $product->price * $cart->no_of_items;
        $cart->save();
    }
  • I have product_id in carts table The problem I'm facing is whenever i tried to update the Quantity i saw error on console mode:

    ErrorException in ProductController.php Trying to get property of non-object

when i dd() the cat_id i see null value or same for all the curt..what could be the possible? thanks

3
  • Look at browser's devtools network. In the request must be your ids. So you can check ajax-request. I think error happened there. Commented Mar 20, 2017 at 8:35
  • In network when i dd(cat_id) i see null Commented Mar 20, 2017 at 8:43
  • No-no, just look at request. For example: imgur.com/a4E1YWt Commented Mar 20, 2017 at 8:46

2 Answers 2

3

The issue is you are not passing the cat_id and qty via the url and is passed via ajax post request

$.ajax({
    url:"{{url('cart/update/{cat_id}/{qty}')}}",  
    method:"POST",  
    data:{
         cat_id : cat_id,
         qty: qty
    },                              
    success: function( data ) {
        // console.log(data);
    }
});

hence the $cat_id and $qty is null in the Controller, so you need to change the code in your controller as

    public function cartUpdate($cat_id, $qty)
    {
        $cart = Cart::find(Input::get('cat_id'));
        $product = Product::find($cart->product_id);
        $cart->no_of_items = Input::get('qty'); 
        $cart->price = $product->price * $cart->no_of_items;
        $cart->save();
    }

Sign up to request clarification or add additional context in comments.

7 Comments

But i get same same cart id for all in the list from view page..Besides it not updated anything ..
Change this in your javascript code var cat_id = parseInt($('.cat_id').val()); to var cat_id = parseInt($(this).prev().val());
When you inspect the cat_id in the DOM? What value are you seeing?
in foreach loop everytime same cat_id printed
Do you get different cat_id when you do dd($carts) outside foreach loop
|
1

Try This Way:

@foreach($carts as $row)
 <input type="hidden" class="cat_id" name="cat_id" value="{{$row->id}}"/>
 <!--Add name="cat_id" AS Attribute -->
<input type="number" class="quantity{{$row->id}}" value="{{$row->no_of_items}}" name="qty" maxlength="3" max="999" min="1" /> &times;${{$row->p_price}}
@endforeach

And Ajax Part :

 @foreach($carts as $row)
    $(".quantity{{$row->id}}").change(updateCart);
 @endforeach

 function updateCart(){
    var qty = parseInt($(this).val());
    var cat_id = parseInt($('.cat_id').val());
    console.log(cat_id);

      $.ajax({ 
                        headers: {
                          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url:'cart/update',  
                        method:"POST",  
                        data:{
                            cat_id:cat_id, 
                            qty:qty
                        },                              
                        success: function( data ) {
                     // console.log(data);
                    }
                   });
} 

2 Comments

same thing coming ..In foreach loop everytime same cat_id get printed.. and value also not getting updated
check ur array $carts, and you also misspelt "@endforeach"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.