[英]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:我正在使用 Laravel sanctum 对我的 API 进行身份验证,并且我想自定义个人访问令牌模型,因此我执行了以下操作:
I created new model named PersonalAccessToken in App namespace.我在 App 命名空间中创建了名为 PersonalAccessToken 的新模型。
I override model used in sanctum to be my new model by adding this line to my AppServiceProvider boot() method.通过将此行添加到我的 AppServiceProvider boot() 方法,我将 sanctum 中使用的模型覆盖为我的新模型。
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class); 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那是因为它的类型暗示是 Laravel\\Sanctum\\PersonalAccessToken 的一个实例
how can I fix that我该如何解决
If you are not extending the default PersonalAccessToken that maybe your issue.如果您没有扩展可能是您的问题的默认PersonalAccessToken 。
Instead of extending Model extend use Laravel\\Sanctum\\PersonalAccessToken而不是扩展模型扩展使用 Laravel\\Sanctum\\PersonalAccessToken
use Laravel\Sanctum\PersonalAccessToken as Model;
class CustomPersonalAccessToken extends Model
{
// Add you customisation here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.