nick
nick

Reputation: 3239

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. 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?

Upvotes: 4

Views: 2057

Answers (3)

nick
nick

Reputation: 3239

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

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

Upvotes: 4

Ariando
Ariando

Reputation: 1421

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

Upvotes: 1

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

You can also use global auth() from view.

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

Upvotes: 2

Related Questions