Call to undefined method App\Models\User::createToken()
When you encounter the error Call to undefined method App\Models\User::createToken() in a Laravel application, it usually points to a mismatch between the code you are running and the version of Laravel you have installed. The snippet provided uses $user->createToken('token'), which is a dynamic method added by the Passport package. If your project does not have Passport correctly configured or if it was never installed, this method simply does not exist on the User model, resulting in an UndefinedMethodException.
Looking at the specific environment detailsâLaravel Framework 8.34.0 and PHP 7.4.3âthe root cause becomes clear: Passport is likely missing or improperly configured. While Laravel 5 included Passport by default, starting with Laravel 6 and continuing through version 8, Passport is no longer part of the core framework. It must be installed explicitly as a separate package.
The user in the source material mentions running php artisan passport:install multiple times. This command generates encryption keys and creates OAuth clients for personal access and password grant. However, simply running that command does not automatically modify your database schema to include the necessary Passport tables (passport_personal_access_clients, passport_personal_access_tokens, etc.). If you ran the command but did not also run the migrations, or if you are using a fresh database where those migrations haven't been applied yet, the API token infrastructure is incomplete.
There is a subtle but critical detail in the provided configuration: the default guard for the web authentication is set to 'driver' => 'session', while the API guard is configured to use Passport. This setup implies an intention to handle stateless API requests alongside traditional session-based web requests. However, the error suggests that even though the auth.php file defines these guards, the underlying user model lacks the trait required to interact with them.
To resolve this, you must ensure that Laravel is aware of the Passport package. If you are on a fresh installation or have cleared your cache after installing Passport, running php artisan config:cache might help if you previously cached an old configuration without Passport. More importantly, verify that the Passport\Passport facade is available or that the necessary traits are imported in your User model.
In Laravel 8, the User model typically needs to import the trait HasApiTokens from the Passport package for the createToken() method to work. If this trait is missing, you can add it manually:
use HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasRoles; // Ensure HasApiTokens is imported via 'use'
// ... rest of the model
}
Alternatively, if you prefer not to rely on traits or if you are using a custom user model that hasn't been updated, you can access the token creation through the Passport facade directly:
$token = \Passport::personalAccessTokens()->create([
'user_id' => $user->id,
'name' => 'API Token',
]);
However, using the trait is the more idiomatic approach in modern Laravel. Another common pitfall involves database migrations. Ensure that you have run:
php artisan migrate --force
This ensures that all tables required by Passport (such as those for personal access tokens) exist in your database. If you are using a custom migration file or have a separate schema management tool, ensure these specific Passport tables are included.
Finally, check your config/auth.php file again. In the provided snippet, the API guard is set to 'driver' => 'passport'. This is correct for Passport-based authentication. However, ensure that you aren't accidentally trying to use Passport tokens with a session guard or vice versa without proper routing logic.
If you are still facing issues after verifying the installation and migrations, consider running php artisan passport:keys --force to regenerate keys if the previous installation failed silently due to existing files. But remember, the primary fix is ensuring the HasApiTokens trait is present on your User model and that Passport is installed via Composer (composer require laravel/passport).