Support the ongoing development of Laravel.io →
Authentication Views
Last updated 1 year ago.
0

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.

Last updated 8 years ago.
0

Thank you very much Xum

0

Sign in to participate in this thread!

Eventy

Your banner here too?

john5db john5db Joined 3 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.