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
Parth kharecha
  • 6,135
  • 4
  • 25
  • 41

10 Answers10

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,
        ],
    ],
Sven Tjeerdsma
  • 283
  • 4
  • 8
  • 2
    Yeah indeed. Nice that they put this in their documentation. Wasted a few hours on this. – Digital Human Nov 04 '20 at 11:48
  • It did not solve my problem. I clear every cache. I'm using v8 :( – hakki Dec 27 '20 at 00:37
  • 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:cache``` – LogGurkha Sep 28 '21 at 13:07
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();
});
linktoahref
  • 7,812
  • 3
  • 29
  • 51
ALI MURTAZA
  • 71
  • 1
  • 2
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

Ramsey Jiang
  • 81
  • 1
  • 7
1

import EnsureFrontendRequestsAreStateful in kernel.php for token verification

use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;

class Kernel extends HttpKernel
{

    'api' => [
               ------
               EnsureFrontendRequestsAreStateful::class,
               ------
             ],
}
hakki
  • 6,181
  • 6
  • 62
  • 106
Parth kharecha
  • 6,135
  • 4
  • 25
  • 41
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.

engrhussainahmad
  • 1,008
  • 10
  • 19
0

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

Example:

Route::middleware('auth:sanctum')->get('products', function() {
      //code
})->name('api.products');
Dharman
  • 30,962
  • 25
  • 85
  • 135
irfanyy
  • 49
  • 5
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

Echo User
  • 11
  • 5
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
Lonare
  • 3,581
  • 1
  • 41
  • 45
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.
Pid
  • 1
0

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.

Edgar
  • 472
  • 6
  • 17