Search code examples
laravellaravel-5eloquentlaravel-authentication

Auth::check() Returns False in Laravel Model but Returns True in Controller


I have a Laravel application where I want to get Auth::user in my model. However, when I do Auth::check() in the model, it returns false whereas in the controller it returns true.

protected static function boot()
{
    parent::boot();

    if (Auth::check()){ //returns false
        // do my stuff
    }
}

Solution

  • I'm pretty sure the boot function on models is executed before the AuthProvider is loaded. Otherwise the default auth provider couldn't be able to use the User model.

    Because of this, you do not have access to the user details in the boot function.