0
votes

i want use laravel sanctum with 2 model

this is code for user model

<?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',
];

}

and usercontroller for function login is :

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

}

and token is true and all route with middleware('auth:sanctum') is working true but i want so use model nurces and write this code first: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';

}

and function login in nurcescontroller is :

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

} when use postman and login with this address - this erorr :

"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))",
1
Column 'tokenable_id' cannot be null :- so either pass the value to it or make that column nullable - Vipertecpro

1 Answers

3
votes

I think, there is an mismatch between primary key of App\Nurces model.

As per documentation and error, two columns values of personal_access_tokens table as follows:

  • tokenable_type - model name e.g App\Model\User
  • tokenable_id - primary key of model

In your case, tokenable_id value trying to insert as NULL, that's why getting error.

Please check and confirm primary key column name of App\Nurces model. Add the below property in the Nurces model class.

protected $primaryKey = 'primary key column name';

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