You could use View Composers
And instead of using the boot method described in the docs you could use:
public function boot()
{
// Using class based composers...
view()->composer(
'*', 'App\Http\ViewComposers\ProfileComposer'
);
// Using Closure based composers...
view()->composer('*', function ($view) {
});
}
That would render whatever variables you declare with
$view->with('yourVariableName', 'yourVariableValue');
to all the views in your app.
Here is a full example of how I used this in one of my projects.
app/Providers/ComposerServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
view()->composer(
'*', 'App\Http\ViewComposers\UserComposer'
);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
app/Http/ViewComposers/UserComposer.php
<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Contracts\Auth\Guard;
class UserComposer
{
protected $auth;
public function __construct(Guard $auth)
{
// Dependencies automatically resolved by service container...
$this->auth = $auth;
}
public function compose(View $view)
{
$view->with('loggedInUser', $this->auth->user());
}
}
Just remember that because you declared a new service provider it needs to be included in the 'providers' array in config/app.php