[英]Shopping cart in laravel, Error at adding product to cart
I am creating Shopping cart. 我正在创建购物车。 While i try to add products to cart, it shows error.
当我尝试将产品添加到购物车时,它显示错误。
My code and errors are as follows. 我的代码和错误如下。
Indirect modification of overloaded property App\\cart::$item has no effect in cart.php (line 31)
间接修改重载属性App \\ cart :: $ item在cart.php中无效(第31行)
productcontroller.php
contains the following code: productcontroller.php
包含以下代码:
public function getAddToCart(Request $request, $id){
$product = Products::findOrFail($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect('jewellery/items');
}
In cart.php contains the following code: 在cart.php中包含以下代码:
public function add($item, $id){
$storedItem = ['qty' => 0, 'price' => $item->net_total, 'item' => $item ];
if($this->items){
if(array_key_exists($id, $this->items)){
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->net_total * $storedItem['qty'];
$this->item[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
Please help me to clear this. 请帮我解决这个问题。
Thanks in advance. 提前致谢。
Have you added __construct() method in your cart model? 您是否在购物车模型中添加了__construct()方法? If not then add it to the and also set items object and give total quantity and price 0. here is the code :
如果没有,则将其添加到中,并同时设置items对象,并给出总数量和价格0。这是代码:
public $items;
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
if ($oldCart) {
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.