Laravel sanctum api_token column not found

76 Views Asked by At

In laravel 10, I want to use sanctum, but when I send the token in the header, it gives this error:

Column not found: 1054 Unknown column 'api_token' in 'where clause' (Connection: mysql, SQL: select * from users where api_token = ld1bmhdVFj6RksdEInavxFvSmVzGfAZD7GGm9mnW12591d0f limit 1)

It looks for the api_token column in the users table! But I didn't see any mention of this column in any training!

api.php

Route::group(['middleware' => 'auth:sanctum'], function () {
    Route::get('user', [UserController::class, 'user'])->name('user');
});

auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
            'provider' => 'users',
        ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],

sanctum.php

'guard' => ['api']

1

There are 1 best solutions below

0
Akash prajapati On

when you use the 'driver' => 'token' then you need to add the below migration:

Schema::table('users', function ($table) {
$table->string('api_token', 80)->after('password')
                    ->unique()
                    ->nullable()
                    ->default(null);
});

this is tokenguard authentication. If you want to use the Sanctum then you need to put the 'driver' => 'sanctum' Let me know if you still face any error.