简体   繁体   English

如何使用 Laravel 中的 Ajax 更新购物车数量

[英]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 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.即使我通过 ajax 调用中的数据发送了 csrf header,我也收到状态代码500 - Internal Server Error

View (cart.blade.php)查看 (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 Ajax 脚本

$(".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 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.我不知道我做错了什么,而且在我刚刚将购物车存放在我的 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,如果您在测试服务器上,请在 your.env 文件中将 debug 设置为 true,您的响应将是更详细的错误消息,或者,
  • take a look at your laravel.log file in storage/logs folder, you will find more detailed info.查看 storage/logs 文件夹中的 laravel.log 文件,您将找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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