2017-10-09 72 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

相关问题