Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel sanctum custom model

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

like image 404
king kong of green land Avatar asked Oct 27 '25 08:10

king kong of green land


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
}
like image 78
the_wizard Avatar answered Oct 30 '25 00:10

the_wizard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!