'Laravel and Passport getting SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'

I've seen many posts about this error, but none of the solutions are working for me.

I'm running Laravel with Passport which is working fine on my development server. As expected, when attempting to check if a user is authenticated on my development server, it returns the catch (inside an axios call):

Development Server

"message":"Unauthenticated."

However, when running the same code on my production server, it returns the catch:

Production Server:

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
in 'where clause' (SQL: select * from `users` where `api_token` = ...

I have have run Passport migration and ['guards']['api']['driver'] set to passport in config/auth.php, and updated the configuration cache that apparently solved the problem for others.

It looks like authentication needs to use the oauth tables from passport migration, but the query appears to be looking at the user table.

EDIT:

I was able to determine that my development server uses the RequestGuard class to find user and my production server uses the TokenGuard class to find user.



Solution 1:[1]

Despite the confusing error, I followed the steps in this post and it correctly now routes the auth request through the RequestGuard and authentication is made as expected.

Not sure how cache can get messed up to cause this, but I'm guessing that my config cache was stuck on web guard and perhaps clearing it now routes correctly through the api guard.

Hope this helps others.

Solution 2:[2]

I have faced the same problem. And I am using Laravel Passport. It all about configuration. got to config/auth.php and change driver name of api array to passport within the guards array

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

    'api' => [
        'driver' => 'passport',//instead of token 
        'provider' => 'users',
        'hash' => false,
    ],
],

Solution 3:[3]

I was facing same problem and i have solve this by

php artisan config:clear
php artisan config:cache
php artisan cache:clear

may be it helpful

Solution 4:[4]

it also may happen when you use wrong auth middleware :

public function __construct()
{
$this->middleware('auth:api');
}

for example for using sanctum :

public function __construct()
{
$this->middleware('auth:sanctum');
}

Solution 5:[5]

there must be a column named api_token in your users table ,
and this column must be filled with unique values for each record , you can add it to your migration like

$table->string(‘api_token’, length)->after( .. ) or create manually

api apps authanticates the users via using the api_token value , instead "id"

Solution 6:[6]

I know the question got answered, but this answer is explained to those who really want to know the real cause of the error in depth.

Remember, it is an error when your route in the API is a protected route and you need to specify a token as a bearer while sending a request after authentication to the protected routes. The below route is protected, if I want to request it I will need an access token that is generated with authentication through the passport.

Route::middleware('auth:api')->group(function () {
    Route::get('/loginCheck', 'Auth\ApiAuthController@LoginCheck');

});

What you need to do is when you change the "token" to "passport" are as follows:

Location of below file: config\auth.php

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

    'api' => [
        'driver' => 'passport',//instead of token 
        'provider' => 'users',
        'hash' => false,
    ],
],

then you will need to run the following commands.

php artisan cache:clear
php artisan route:clear
php artisan config:cache

now start the laravel server again

 php artisan serve

And you are done!

Solution 7:[7]

Try running:

php artisan config:cache
php artisan cache:clear
php artisan config:clear

Solution 8:[8]

I have been solved it by running the following commands in the composer window

  1. php artisan key:generate
  2. php artisan config:cache
  3. php artisan cache:clear
  4. php artisan config:clear

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ziaur Rahman
Solution 3
Solution 4
Solution 5 ulas korpe
Solution 6
Solution 7 Leotrin Elmazi
Solution 8 Md Saidur Rahman