Rashad Mirza logo
How to get Auth user info on Laravel Controller construct

How to get Auth user info on Laravel Controller construct

Rashad Mirza Dec 5, 2020
If you try to get Auth::user() inside Laravel -> Controller -> __construct, it will return null.
The reason is middleware is not running yet. Constructor always are created before the middleware.

But, hopefully there is simple way to do it using middleware:

public function __construct()
{
$this->middleware(function ($request, $next) {
$user = Auth::user();
return $next($request);
});
}