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

13,317

Solution 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

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

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

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"

Share:
13,317
mjpsr11
Author by

mjpsr11

Updated on June 23, 2022

Comments

  • mjpsr11
    mjpsr11 almost 2 years

    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.