当购物车空了时,Laravel 5总是出现错误(Always getting error when cart is empty, Laravel 5)

编程入门 行业动态 更新时间:2024-10-28 15:23:17
购物车空了时,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

更多推荐

本文发布于:2023-07-17 07:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:购物车   空了   出现错误   cart   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!