Normally “Call to undefined method App\Models\User::createToken()” error comes during working on Passport API authentication.
So in this blog post, we solve this issue in simple two-line. just flow me.
App\Models\User.php
Add method createToken is in HasApiTokens trait, you should use it In your User Model.
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens; /** ADD THIS LINE **/
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable; /** ADD THIS LINE **/
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
At the above code, I added two line
use Laravel\Passport\HasApiTokens; /** ADD THIS LINE **/
and
use HasApiTokens, HasFactory, Notifiable; /** ADD THIS LINE **/
I hope it will help you.

Brijpal Sharma is a web developer with a passion for writing tech tutorials. Learn JavaScript and other web development technology.