Reputation: 10882
I have a user model class that has class User extends Authenticatable
and another model class that I also created that has class Foo extends Model
it's causing some issues in displaying data from the routes files and I am pretty sure it has something to do with the 'Authenticatable' part because for Foo, the information displays correctly, but for User, it doesn't, even with the same code.
What's the difference between these two classes/models?
Upvotes: 10
Views: 12868
Reputation: 28841
If you look at the imports it shows:
use Illuminate\Foundation\Auth\User as Authenticatable;
This means Authenticatable
is an alias to Illuminate\Foundation\Auth\User
.
If you look into the source code of Illuminate\Foundation\Auth\User
it shows:
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
Basically Authenticatable
is the same as a normal model, but adds the following traits:
Authenticatable
Authorizable
CanResetPassword
MustVerifyEmail
This is used by authentication, and adds the following methods:
getAuthIdentifierName()
getAuthIdentifier()
getAuthPassword()
getRememberToken()
setRememberToken()
getRememberTokenName()
This is used to determine if a User is able to perform certain abilities. It has the following methods:
can()
canAny()
cant()
cannot()
Used for resetting passwords. It has the following methods
getEmailForPasswordReset()
sendPasswordResetNotification()
Used for verifying emails if your using that feature. It has the following methods:
hasVerifiedEmail()
markEmailAsVerified()
sendEmailVerificationNotification()
getEmailForVerification()
I recommend try having a look at the source code yourself to get a better understanding what these traits do.
Also, based on the above, I doubt it will have any issues with your data in the route files.
Upvotes: 6
Reputation: 1864
When you extend the Authenticatable, you are getting more features like authentication and authorization and registering stuff. So whenever you have users or customers or accounts refer to persons, it's good to create a new model that extends the Authenticatable.
When you extend just the Model, you are creating a class or type just as a reflection of some kind of resource you have in your application.
Try to locate the definition of the Authenticatable and you will see:
The Illuminate\Foundation\Auth\User as Authenticatable is just a class that tacks on some additional traits Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail.
The underlying Illuminate\Foundation\Auth\User class also extends model giving you all the relational functionality you would expect if you were to simply extend Model.
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
Upvotes: 0