0

我想用 2 个模型使用 laravel sanctum

这是用户模型的代码

<?php namespace App; 
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens,Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
   'id', 'name', 'family','gender','birhday','national_code','email','mobile','profie_pic','province_code','city_code','address','username','role','status','password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token','role',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

功能登录的用户控制器是:

public function login(Request $request) { 
$user= User::where('email', $request->email)->first();
if (!$user || !Hash::check($request->password, $user->password)) {
    return response([
        'message' => ['These credentials do not match our records.']
    ], 404);
}

$token = $user->createToken('my-app-token')->plainTextToken;
$response = [
    'user' => $user,
    'token' => $token
];
 return response($response, 201);

}

并且令牌是真实的,并且带有中间件('auth:sanctum')的所有路由都在正常工作,但我想使用模型护士并首先编写此代码:模型护士:

class Nurces extends model
{
use HasApiTokens,Notifiable;

protected $fillable = [
    'name', 'family','gender','birhday','national_code','email','mobile','profie_pic','province_code','city_code','address','username','status','password',
 ];

 /**
  * The attributes that should be hidden for arrays.
  *
  * @var array
  */
 protected $hidden = [
     'password','remember_token',
 ];
 protected $primaryKey = 'nurces_id';

}

nurcescontroller 中的登录功能是:

public function login(Request $request) { 
$nurces= Nurces::where('email', $request->email)->first();

if (!$nurces || !Hash::check($request->password, $nurces->password)) {
    return response([
        'message' => ['These credentials do not match our records.']
    ], 404);
}

 $token = $nurces->createToken('my-nurces-token-')->plainTextToken;

$response = [
    'nurces' => $nurces,
    'token' => $token
];

 return response($response, 201);

} 当使用邮递员并使用此地址登录时 - 此错误:

"message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tokenable_id' cannot be null (SQL: insert into `personal_access_tokens` (`name`, `token`, `abilities`, `tokenable_id`, `tokenable_type`, `updated_at`, `created_at`) values (my-nurces-token-, fa55707e2bd9e1b71f8e5ebc0623f9ce1cc8e49f5b6e1ff804dda262e93811f4, [\"*\"], ?, App\\Nurces, 2020-07-05 11:18:58, 2020-07-05 11:18:58))",
4

1 回答 1

3

我认为, App\Nurces模型的主键之间存在不匹配。

根据文档和错误,personal_access_tokens表的两列值如下:

  • tokenable_type - 模型名称, 例如 App\Model\User
  • tokenable_id - 模型的主键

在您的情况下,tokenable_id值试图插入为NULL,这就是出错的原因。

请检查并确认App\Nurces模型的主键列名。在Nurces模型类中添加以下属性。

protected $primaryKey = '主键列名';

https://laravel.com/docs/7.x/eloquent

于 2020-07-14T19:52:24.613 回答