当购物车空了时,Laravel 5总是出现错误(Always getting error when cart is empty, Laravel 5)
面对这个错误:
CartController.php中的ErrorException 35行:尝试获取非对象的属性
这是代码:
public function index() { $this->data['details'] = Cart::content(); $this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first(); $regType = session('regType'); $regId = session('id_wilayah'); $qReg = RegionCategory::find($regId); if($regType == 'children') { $this->data['minimalWeight'] = $qReg->minimal_weight; //$this->data['minimalBuy'] = $qReg->min_buy; } $this->data['minimalBuy'] = $qReg->min_buy; //this is line 35 $this->data['regType'] = $regType; \Session::put('price',Cart::total()); \Session::put('totalPrice',Cart::total()); \Session::put('paycode',0); return view('client.carts.index',$this->data); }当我删除第35行时,错误消失了,但该代码对于使用最小购买过滤器很重要。 如何解决这个错误?
Facing this error:
ErrorException in CartController.php line 35: Trying to get property of non-object
This is the code:
public function index() { $this->data['details'] = Cart::content(); $this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first(); $regType = session('regType'); $regId = session('id_wilayah'); $qReg = RegionCategory::find($regId); if($regType == 'children') { $this->data['minimalWeight'] = $qReg->minimal_weight; //$this->data['minimalBuy'] = $qReg->min_buy; } $this->data['minimalBuy'] = $qReg->min_buy; //this is line 35 $this->data['regType'] = $regType; \Session::put('price',Cart::total()); \Session::put('totalPrice',Cart::total()); \Session::put('paycode',0); return view('client.carts.index',$this->data); }when I delete the line 35 the error is gone, but that code is important for using minimal buy filter. How to resolve this error?
最满意答案
这是错误原因$ qReg-> min_buy返回null。 你可以通过像这样返回默认值来修复:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;这意味着如果是null返回0
That's error cause $qReg->min_buy return null. You can fix by return default value like this:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;That's mean if is null return 0
更多推荐
发布评论