Define a global variable to be used in several views independent of the controller of each view

1

I need to declare a globlal variable, something similar to:

$user = Auth::user();

I am consuming an API and I need to get the $user variable in the login form in a global way to use it in the whole app. I am not very familiar with this topic as I saw something done as Laravel/passport , but I do not know if there is another method to achieve it.

In short, what I need is: When logging in, use the return variable $user globally.

How can I do it?

    
asked by Cesar Augusto 03.08.2017 в 00:49
source

1 answer

2

Add in your app/Providers/AppServiceProvider.php the authenticated user to all the views that are loaded.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Auth;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        view()->composer('*', function($view) {
            $view->with('user', Auth::user());
        });
    }
}
    
answered by 03.08.2017 в 01:09