2017-05-15 15 views
0

面對這樣的錯誤:總是得到錯誤的時候車是空的,Laravel 5

ErrorException in CartController.php line 35: Trying to get property of non-object

這是代碼:

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,但該代碼對使用最小購買過濾器非常重要。 如何解決此錯誤?

+2

什麼是$這個 - >數據?哪條線是35線? –

回答

0

這是錯誤原因$ qReg-> min_buy返回null。 你可以通過這樣的返回默認值修正:

$this->data['minimalBuy'] = $qReg->min_buy ?? 0; 

這意味着,如果是返回0

+0

它的工作,非常感謝你:) –

+0

隨時歡迎 – MohamedSabil83

+0

恩對不起,但是當我嘗試在登臺服務器上出現這個錯誤:local.ERROR:異常'Symfony \ Component \ Debug \ Exception \ FatalErrorException'帶有消息'語法錯誤,意外的'?''在/home/paskomnas-dev/www/app/Http/Controllers/Client/CartController.php:38中 –

0

這就是說,$ qReg不是一個對象,而是試圖使用min_buy屬性從它獲取值。

嘗試使用print_r($qReg);打印對象$qReg,然後退出腳本。

print_r($qReg); 
exit(); 

並檢查該對象是否具有屬性min_buy或不。

爲什麼$this->data['minimalBuy'] = $qReg->min_buy; if block後?即使if($regType == 'children')爲假,它也會被覆蓋。