Laravel Sanctum : column not found: 1054 Unknown column 'api_token' in 'where clause'

Viewed 5397

package: Sanctum

After generate token when request for get data its throw me error like this

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token' in   
'where clause' (SQL: select * from `users` where `api_token` = XAzuNGUeOJ8CXbIoGEWhBTtWIFr0lFr8jjwScXQ4B0Qxfmu
2cHm9LaUwGX96zy0AnXhLLcCnBFCodQaOlimit 1) in file
9 Answers

go to config/auth.php

and change the api array in guards to sanctum example:

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

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

Go to routes/api.php and use this

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

instead of

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

In the api.php

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

In the config/auth.php

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

After that, it will fix the issue. I have uploaded the code to my GitHub.https://github.com/ramseyjiang/laravel_8_api

import EnsureFrontendRequestsAreStateful in kernel.php for token verification

use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;

class Kernel extends HttpKernel
{

    'api' => [
               ------
               EnsureFrontendRequestsAreStateful::class,
               ------
             ],
}

Run the migration and then check, if there was still the issue then either users table don't have api_token field or it's not defined in the User model inside $fillable array.

You can try add "middleware(auth:sanctum)" for your api route.

Example:

Route::middleware('auth:sanctum')->get('products', function() {
      //code
})->name('api.products');

Run

  1. php artisan cache:clear
  2. php artisan config:clear
  3. php artisan route:clear

Then restart Apache and Mysql (for XAMPP/WAMPP) and run artisan serve command again it worked for me.make sure you read their docs first

1.So to answer this question, after racking my brain, I decided to clear the application, configuration, and route caches,that did the trick for me.

  php artisan cache:clear

2.You can run the above statement in your console when you wish to clear the application cache. What it does is that this statement clears all caches inside storage\framework\cache.

  php artisan route:cache

3.This clears your route cache. So if you have added a new route or have changed a route controller or action you can use this one to reload the same.

  php artisan config:cache
  • First drop table personal_access_tokens
  • Second delete row that contain "%create_personal_access_tokens_table%" in your table migrations.
  • Third execute again php artisan migrate.
Related