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
相关产品推荐
相关产品推荐

