[英]Trying to get property of non-object [Laravel] when trying to access Auth::user()->name on view
I'm trying to get username from view using: 我正在尝试从视图中获取用户名:
Auth::user()->name
and I'm getting 而且我正在
Trying to get property of non-object
because Auth::user()
is NULL. 因为Auth::user()
是NULL。 How is this possible? 这怎么可能? The navbar laravel generates uses the same and it works there. 导航栏laravel生成使用相同,它在那里工作。
EDIT: It seems that I can't even get the auth user from the Controller. 编辑:似乎我甚至无法从Controller获得auth用户。
Auth::guard('admin')->check(); //true
Auth::user(); //null
Any ideas? 有任何想法吗?
好吧,我再次弄清楚了,因为我使用了自定义防护,我实际上需要使用:
Auth::guard('admin')->user()->name;
您还可以从视图中使用全局auth()
。
{{auth()->user()->name}}
在尝试使用{{Auth::user()->name}}
访问视图之前,请确保您已登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.