Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between 'extends Authenticatable' vs 'extends Model' in Laravel?

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?

like image 556
Simon Suh Avatar asked Oct 06 '16 04:10

Simon Suh


1 Answers

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

Illuminate\Auth\Authenticatable

This is used by authentication, and adds the following methods:

  • getAuthIdentifierName()
  • getAuthIdentifier()
  • getAuthPassword()
  • getRememberToken()
  • setRememberToken()
  • getRememberTokenName()

Illuminate\Foundation\Auth\Access\Authorizable

This is used to determine if a User is able to perform certain abilities. It has the following methods:

  • can()
  • canAny()
  • cant()
  • cannot()

Illuminate\Auth\Passwords\CanResetPassword

Used for resetting passwords. It has the following methods

  • getEmailForPasswordReset()
  • sendPasswordResetNotification()

Illuminate\Auth\MustVerifyEmail

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.

like image 72
Yahya Uddin Avatar answered Oct 09 '22 21:10

Yahya Uddin