DEV Community

Tr.Ra
Tr.Ra

Posted on

get user in AppServiceProvider Laravel

Laravel session is initialized in a middleware so you can't access the session from a Service Provider, because they execute before the middleware in the request lifecycle

If for some other reason you want to do it in a service provider, you could use a view composer with a callback, like this:

view()->composer('*', function($view)
{
if (Auth::check()) {
// $view->with('currentUser', Auth::user());
dd(Auth::user());
}else {
// $view->with('currentUser', null);
dd('nope not logged in');
}
});

Top comments (0)