简体   繁体   中英

How to update cart quantity with Ajax in Laravel

I am using a package from https://packagist.org/packages/bumbummen99/shoppingcart which extends Crinsane/LaravelShoppingCart in my Laravel 7.3 framework and I am struggling with updating my cart quantity with Ajax straight in my cart view.

I am getting a Status Code of 500 - Internal Server Error even though I sent the csrf header through my data in ajax call.

View (cart.blade.php)

<tbody>
 @if(Cart::count() > 0)
 @foreach(Cart::content() as $details)
    <tr id="product-show">
        <td data-th="Product">
        <div class="row">
            <div class="col-sm-3 hidden-xs img-responsive"><img src="img/{!!$details->options->image!!}" width="100" height="100"/></div>
                <div class="col-sm-9">
                    <h4 class="nomargin">{{ $details->name }}</h4>
                </div>
            </div>
        </td>
        <td data-th="Price">{{ $details->price }} RON</td>
        <td data-th="Quantity">
            <input type="number" value="{{ $details->qty }}" class="form-control quantity" class="quantity"/>
        </td>
        <td data-th="Subtotal" class="text-center" id="total-price">{{ $details->price * $details->qty }} RON</td>
        <td class="actions text-center" data-th="">
            <button class="btn btn-info btn-sm update-cart" data-token="{{ csrf_token() }}" data-id="{{ $details->rowId}}" style="margin: 10px;">
            <i class="fa fa-refresh"></i> Refresh</button>
            <button class="btn btn-danger btn-sm remove-from-cart" data-token="{{ csrf_token() }}" data-id="{{ $details->rowId}}" style="margin: 10px;">
            <i class="fa fa-trash-o"></i>Delete</button> 
        </td>
    </tr>
 @endforeach
 @endif
 </tbody>

Ajax script

$(".update-cart").click(function (e) {
        e.preventDefault();
        var ele = $(this);
        $.ajax({
        url: "{{ url('update-cart') }}",
        method: "patch",
        data: {_token: '{{ csrf_token() }}', id: ele.attr("data-id"), quantity:
        ele.parents("tr").find(".quantity").val()},
        success: function (response) {
            window.location.reload(); 
        }
    });
 });

Controller

public function updateCart(Request $request){
        
        $cart = Cart::content()->where('rowId', $request->id);
        //update quantity
        //dd($cart);
        return view('pages.cart')->with('cart-success', 'Cart updated');
    }

Route

Route::patch('update-cart', 'ProductController@updateCart');

I have no idea what am I doing wrong and moreover it worked fine before when I was just storing the cart in my session.

I would appreciate any advice, I have no idea how to fix this. Thanks guys !

  • if you are on a test server, set debug to true in your.env file, and thee the response will be a more detailed error message or,
  • take a look at your laravel.log file in storage/logs folder, you will find more detailed info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM