Friday, March 31, 2017

Laravel 5.4 : Get logged in user id inside __construct()

I am trying to access Auth::user()->id; inside constructor but it always return the error Trying to get property of non-object. I study in the laravel documentation that Session is not accessible inside constructor and also search on SO for this. I need logged in user id inside constructor because I have to fetch data from database and make it available for all its method. My current code is :

public function __construct(){
    $this->middleware('auth');
    $induction_status = TrainingStatusRecord::where('user_id',Auth::user()->id)->where('training','=','induction')->get();
    View::share('ind_status',$induction_status);
}  

Is there any way (easy way) to get logged in user id inside constructor.

I will appreciate any help.

Thank You



via Sanchit Gupta

Advertisement