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)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay