@domenico
To share the authenticated user across all views in Laravel, you can use view composers. View composers are callbacks or class methods that are called when a view is rendered. They allow you to share data with multiple views.
Follow these steps to share the authenticated user:
1 2 3 4 5 6 7 8 9 |
use IlluminateSupportFacadesView;
use IlluminateSupportFacadesAuth;
public function boot()
{
View::composer('*', function ($view) {
$view->with('currentUser', Auth::user());
});
}
|
This code registers a view composer that runs for all views ('*'). It shares the logged-in user object (retrieved using Auth::user()) with the variable currentUser.
Now, you can access the authenticated user in any view by using the $currentUser variable. For example, in a view file, you can access the user's email as follows:
1
|
{{ $currentUser->email }}
|
Make sure to check if the user is authenticated before accessing the properties.