Trying to get property of non-object [Laravel] when trying to access Auth::user()->name on view

Viewed 1922

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. How is this possible? The navbar laravel generates uses the same and it works there.

EDIT: It seems that I can't even get the auth user from the Controller.

Auth::guard('admin')->check(); //true
Auth::user(); //null

Any ideas?

3 Answers

Well I figured it out again, since I used a custom guard, I actually needed to use:

Auth::guard('admin')->user()->name;

You can also use global auth() from view.

{{auth()->user()->name}}

Make sure that you are logged in before trying to access a view with {{Auth::user()->name}}.

Related