Laravel 5: Global Auth $user variable in all controllers

14,864

Solution 1

Has shown in other answers, you can:

  • set it as a property of your base controller (I don't recommend this, it just adds clutter)
  • create a custom helper (see below)
  • just do \Auth::user()


Another technique, you can use auth() helper:

auth()->user()

It is even documented.


According to this PR, there is no plan to add an user() helper in Laravel.

If you consider it is worth adding this helper to your project, here is the code:

function user()
{
    // short and sweet
    return auth()->user();

    // alternative, saves one function call!!
    return app('Illuminate\Contracts\Auth\Guard')->user();
}

Solution 2

I do not see why something like this would not work:

public function __construct()
{
    $this->user = \Auth::user();
    \View::share('user', $this->user);
}

This way it's available to all your views and as long as your controllers are extending BaseController, it's available in all your controllers as well with $this->user.

Solution 3

Why don't you use a helper function. It won't give you a $user variable, but it can give you a more cleaner way to accomplish this.

In your helpers.php add a function:

function getUser(){
    return Auth::user();
}

In all other places (controller, views, models) call: getUser(). That's it.

If you don't have any helper file for your functions yet. You can follow this link to create one.

Solution 4

I don't see whats the issue with using Auth::user(), this class is using singleton pattern which means that if the Auth::user() is already initialized it won't check again against the database especially when most of the code shouldn't even be inside a controller so basically you would still need to do Auth::user() inside your services, models, form requests, etc..

abstract class Controller extends BaseController {
    protected $user;
    use ValidatesRequests;
    public  function ___construct(){
        $this->user = \Auth::user();
    } 
}
Share:
14,864
Chad Priddle
Author by

Chad Priddle

Updated on June 04, 2022

Comments

  • Chad Priddle
    Chad Priddle almost 2 years

    In my BaseController I have this:

    public function __construct()
    {
        $user = Auth::user();
        View::share('user', $user);
    }
    

    I can access the $user array throughout all of my views but what if I want it to be a global variable available in all of my Controllers?

    I have the following working code:

    use Auth;
    use DB;
    
    class UsersController extends BaseController
    {
       public function index()
       {
            $user = Auth::user();
            $users = DB::table('users')->where('user_id', $user->user_id)->get();
            return view('pages.users.index', compact('users');
        }
    }
    

    I want the $user variable available in all of my controllers and within all of the public functions within each on the controllers but I don't want to have to redeclare "use Auth;" and "$user = Auth::user();"

    I tried adding it to a __construct in the BaseController but that didn't seem to work, Undefined $user variable error. Any thoughts? I know it's possible.