2017-10-09 68 views
1
public function __construct() 
{ 
    view()->share('tags', DB::table('tags')->where('view', Auth::user()->view)->get()); 
    $this->middleware('auth'); 
} 

我收到:Laravel 5.5試圖讓非對象的屬性

(1/1)ErrorException 試圖讓非對象

爲什麼財產?當我嘗試return Auth::user()->view;時,我得到了。但是當我手動輸入->where('view', 2)那麼一切都很好。我怎樣才能解決這個問題?提前致謝。

回答

1

嘗試返回gettype (Auth::user()->views),看是否其意見的整數。如果不是隻有(int)Auth::user()->views就可以了。

或嘗試將其分配到這樣一個變量:

$views = (int)(Auth::user()->views); 

然後用它...... ->where('view', $views)->get());

+0

您最好將字段類型更改爲數據庫中的整數或你可能會再次遇到同樣的問題。 – Onix

1

您使用的身份驗證的中間件之後驗證::用戶() - >查看它是錯的

public function __construct() 
{ 
    $this->middleware('auth'); // firstly auth check , after that can use Auth::user() , before them Auth::user() return null and you cant use ->view on null , when ErrorException Trying to get property of non-object 
    view()->share('tags', DB::table('tags')->where('view', Auth::user()->view)->get()); 
} 

用戶未登錄請

驗證::檢查( )

之後使用

驗證::用戶() - >視圖

+0

是的,我已經檢查。在__construct()中我收到false。在另一個功能是真的。 – DisplayName

+0

我已經更改了代碼位置,無論如何.. – DisplayName

+0

on __construct請dd(Auth :: user())並查看結果如果返回null,則必須檢查中間件 – honarkhah

相關問題