Laravel sanctum custom model

Viewed 1679

I'm using Laravel sanctum to authenticate my API, and I wanted to customize personal access token model so I did the following:

  • I created new model named PersonalAccessToken in App namespace.

  • I override model used in sanctum to be my new model by adding this line to my AppServiceProvider boot() method.

    Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);

but when I create a token it works fine and insert it into DB but this line throw exception

return new NewAccessToken($token, $token->id.'|'.$plainTextToken);

and that's because it's type hinted to be an instance of Laravel\Sanctum\PersonalAccessToken

how can I fix that

1 Answers

If you are not extending the default PersonalAccessToken that maybe your issue.

Instead of extending Model extend use Laravel\Sanctum\PersonalAccessToken

use Laravel\Sanctum\PersonalAccessToken as Model;

class CustomPersonalAccessToken extends Model
{
     // Add you customisation here
}
Related