laravel 5.4 : cant access Auth::user() in the __construct method

Viewed 14621

In previous versions of Laravel, in the controllers which I needed to access logged user in all the methods I used to do something like this:

class DashboardController extends Controller
{
    private $user ;
    function __construct(Request $request)
    {
        $this->middleware('auth');
        $this->user = \Auth::user();
    }

    function func_1(){
      $objects = Objects::where('user_id' , $this->user->id )->get();
    }
    function func_2(){
      $objects = Objects::where('user_id' , $this->user->id )->get();
    }
    function func_3(){
      $objects = Objects::where('user_id' , $this->user->id )->get();
    }

Mostly because I don't like the default syntax \Auth::user() but after upgrading to 5.4 this doesn't work anymore and I get null from $this->user

It works fine in other methods though. Basically \Auth::user() return null in the __construct method but works fine in the other functions.

2 Answers
Related