Home > Blockchain >  Creating Token with Sanctum in Laravel 9 with no 'expires_at' column
Creating Token with Sanctum in Laravel 9 with no 'expires_at' column

Time:08-04

I installed Laravel 9 and Sanctum, made a migration and tried to create token with 'createToken' method from User class which extends from Authenticatable. It's all from Laravel and Sanctum instalation. I used code below.

createToken('secrettoken')->plainTextToken;

And I've got an error message:

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'expires_at' in 'field list' (SQL: insert into `personal_access_tokens` (`name`, `token`, `abilities`, `expires_at`, `tokenable_id`, `tokenable_type`, `updated_at`, `created_at`) values (secrettoken, 4afa89fe2706656efae648c43c2a451b5b6d10be8b4e4558b6f9097706f814eb, [\"*\"], ?, 14, App\\Models\\User, 2022-08-02 19:30:00, 2022-08-02 19:30:00))

It seems 'createToken' method wants to fullfil an non-existing 'expires_at' column and doesn't have a value. This method has two arguments: name and abilitie (optional).

Did anyone has the same problem as me? Maybe I'm doing some wrong or using this method wrong.

CodePudding user response:

I had the same issue today, I checked the createToken method located at vendor/laravel/sanctum/src/HasApiTokens.php. Sanctum package has added expires_at column in this method, but didn't add that column to the tokens migration file.
enter image description here
So, I just add the expires_at column to their migration file.

public function up()
{
    Schema::create('personal_access_tokens', function (Blueprint $table) {
        $table->id();
        $table->morphs('tokenable');
        $table->string('name');
        $table->string('token', 64)->unique();
        $table->text('abilities')->nullable();
        // Add here
        $table->timestamp('expires_at')->nullable();
        $table->timestamp('last_used_at')->nullable();
        $table->timestamps();
    });
}

And it worked.

  • Related