You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel8中darryldecode/laravelshoppingcart商品数量设置无效解决方法

问题解决:darryldecode/laravelshoppingcart 商品数量设置不生效
  • 核心原因:你代码中使用的quantity字段名不符合包最新版本的参数要求,最新版(v4+)的Cart::add()方法中,数量对应的参数名是qty,而非quantity,框架无法识别该字段,导致商品项中没有数量信息。

  • 修改后的代码:

public function add(Request $request)
{
    $product = Product::findOrFail($request->product_id);
    $rowId = $product->id;
    
    Cart::add([
        'id' => $rowId,
        'name' => $product->name,
        'price' => $product->is_sale ? $product->sale_price : $product->price,
        'qty' => 1, // 将quantity替换为qty
        'attributes' => $product->toArray(),
        'associatedModel' => $product
    ]);
    
    return redirect()->back();
}
  • 验证方法:修改后重新添加商品,通过dd(Cart::content())调试,就能看到商品项中包含qty字段,数量设置也会正常生效。

内容的提问来源于stack exchange,提问作者MR Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 10:45:33