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

10

11

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
Lookin answered 10/4, 2020 at 13:21 Comment(6)
Does the api_token column exist on the users table?Apterygial
Perhaps forgot a migration?Cameliacamella
the same bug, did you resolve issue?Lie
import EnsureFrontendRequestsAreStateful in kernel.php for token verification. use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStatefulin api sectionLookin
did you resolve this issue? @ParthkharechaInformal
@hakiko Yes check this link https://mcmap.net/q/957333/-laravel-sanctum-column-not-found-1054-unknown-column-39-api_token-39-in-39-where-clause-39Lookin
M
27

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,
        ],
    ],
Mercuric answered 7/8, 2020 at 12:38 Comment(3)
Yeah indeed. Nice that they put this in their documentation. Wasted a few hours on this.Hughs
It did not solve my problem. I clear every cache. I'm using v8 :(Informal
Thanks! In the latest laravel version, they have removed the API guard as default. Thanks, it works. php artisan config:clear php artisan cache:clear php artisan config:cacheLipophilic
S
7

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();
});
Sunlit answered 7/7, 2020 at 18:54 Comment(0)
P
3

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

Portamento answered 3/5, 2021 at 8:3 Comment(2)
While this code may answer the question, adding some explanation on how and why it solves it will improve the quality of your answerYogini
@ᴄʀᴏᴢᴇᴛ you need to make sure that the api -> driver is set to sanctumGuesswork
L
1

import EnsureFrontendRequestsAreStateful in kernel.php for token verification

use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;

class Kernel extends HttpKernel
{

    'api' => [
               ------
               EnsureFrontendRequestsAreStateful::class,
               ------
             ],
}
Lookin answered 28/12, 2020 at 4:0 Comment(0)
V
1

In my case I had messed with the config\sanctum.php file. Specifically I had changed the guard to 'guard' => ['api'],.

reverting to 'guard' => ['web'], fixed it. Not sure why I changed it in the first place tbh.

Verify answered 2/2, 2023 at 13:0 Comment(0)
D
0

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.

Dissonant answered 10/4, 2020 at 14:0 Comment(0)
G
0

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

Example:

Route::middleware('auth:sanctum')->get('products', function() {
      //code
})->name('api.products');
Guile answered 16/4, 2021 at 12:54 Comment(0)
P
0

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

Phip answered 14/7, 2021 at 9:12 Comment(1)
This is a typical cache clearing method, but how does it help solve the problem posed?Scant
S
0

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
Soprano answered 19/7, 2021 at 11:51 Comment(0)
S
0
  • 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.
Saliferous answered 9/8, 2022 at 22:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.