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);

}

トークンは true で、ミドルウェア ('auth:sanctum') を含むすべてのルートは true で動作していますが、モデル nurces を使用して最初にこのコードを記述したい:model nurces:

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