I am trying to access auth()->user() in controller constructor, but it always return null
.
I have tried below, but no luck!
protected $user;
function __construct() {
$this->middleware(function ($request, $next) {
$this->user = auth()->user();
return $next($request);
});
}
Is there any way to do this?
--Thanks
Controller Constructor is called before Middlewares. So you can not get User information inside Constructor().
My advice is create private function that sets User, and call this inside your functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With