I had a bit confusion in authentication, i'm a Laravel user since version 5.2. But in this scenario on version 5.6, I create a middleware that checks the authenticated users, the logout function was perfect but, i try to access the page with my middleware, it returns
Trying to get property 'name' of non-object
Because i had a function that fetches the name of Authenticated User.
How can i handle this scenario?
My Middleware code:
public function handle($request, Closure $next) {
if (Auth::check()) {
return $next($request);
}
return redirect()->route('login');
}
In the page:
{{Auth::user()->name}}
Thanks
You get this error because the current user is not authenticated. You can use the optional()
helper:
{{ optional(auth()->user())->name }}
Or:
{{ auth()->check() ? auth()->user()->name : 'Please login' }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.