我想用 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))",