laravel 5.5 Get user details inside constructor

If you are using the same user table for both "front-end" user and "admin" & want to apply condition in admin controller's constructor.

You can use below.

auth()->user()

And in the constructor you can use below code.

public function __construct(){      
    $this->middleware(function ($request, $next) {      
        if(auth()->user()->hasRole('frontuser')){
            return redirect()->route('home')->withFlashMessage('You are not authorized to access that page.')->withFlashType('warning');
        }
        return $next($request);
    });
}

But I prefer to handle these in separate middleware class instead of writing this in the controllers constructor.


That's because constructors are created before middlewares,that's why its returning null.

This answer will propably solve your problems: Can't call Auth::user() on controller's constructor