Laravel 5, View::Share

I am using Laravel 5.0.28, view::share('current_user', Auth::User()) doesn't work anymore because this issue https://github.com/laravel/framework/issues/6130

What I do instead is, first create a new service provider using artisan.

php artisan make:provider ComposerServiceProvider

Then add ComposerServiceProvider to config/app.php providers array

//...
'providers' => [
    //...
    'App\Providers\ComposerServiceProvider',
]
//...

Then open app/Providers/ComposerServiceProvider.php that just created, inside boot method add the following

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    View::composer('*', function($view)
    {
        $view->with('current_user', Auth::user());
    });
}

Finally, import View and Auth facade

use Auth, View;

For more information, see http://laravel.com/docs/5.0/views#view-composers


First, you can probably create your own BaseController and extend it in other controllers.

Second thing is, that you may use Auth:user() directly in View, you don't need to assign anything in the view.

For other usages you can go to app/Providers/App/ServiceProvider.php and in boot method you can View::share('current_user', Auth::User()); but of course you need to add importing namespaces first:

use View;
use Auth;

because this file is in App\Providers namespace


This will may help:

App::booted(function()
{
    View::share('current_user', Auth::user());
});