If current userid and username is all you need, just use Auth::user()->id
and Auth::user()->name
in a view.
As for why View::share()
is not working for you, it's because at the time routes.php
is executed (as part of registering and booting service providers) current user is not yet set, because sessions are handled by a middleware, and middlewares are run after service providers.
Another way to solve your issue is by using wildcard view composers. Add these lines to boot
method of your App\Providers\AppServiceProvider
class:
\View::composer('*', function($view)
{
$view->with('currentUser', \Auth::user());
});
This closure will be executed when rendering a view, and by that time session will already be started.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community