4

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Chad Priddle
  • 650
  • 2
  • 15
  • 32

4 Answers4

3

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.

user1669496
  • 32,176
  • 9
  • 73
  • 65
3

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();
}
Gras Double
  • 15,901
  • 8
  • 56
  • 54
2

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.

Community
  • 1
  • 1
Mustafa Ehsan Alokozay
  • 5,583
  • 2
  • 24
  • 30
1

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();
    } 
}
Gal Sisso
  • 1,939
  • 19
  • 20