Laravel 和 Passport 获取 SQLSTATE[42S22]:未找到列:1054 未知列 'api_token'
Laravel and Passport getting SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
我看过很多关于此错误的帖子,但是 none 的解决方案对我有用。
我 运行正在 Laravel 使用 Passport,它在我的开发服务器上运行良好。正如预期的那样,当尝试检查用户是否在我的开发服务器上经过身份验证时,它 returns 捕获(在 axios 调用中):
开发服务器
"message":"Unauthenticated."
但是,当 运行在我的生产服务器上使用相同的代码时,returns 问题:
生产服务器:
"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
in 'where clause' (SQL: select * from `users` where `api_token` = ...
我有 运行 护照迁移并且 ['guards']['api']['driver'] 在 config/auth.[=48 中设置为护照=],并更新了显然为其他人解决了问题的配置缓存。
看起来身份验证需要使用护照迁移中的 oauth tables,但查询似乎是在查看用户 table。
编辑:
我能够确定我的开发服务器使用 RequestGuard
class 来查找用户,而我的生产服务器使用 TokenGuard
class 来查找用户。
尽管出现了令人困惑的错误,但我还是按照步骤 进行了操作,现在它通过 RequestGuard 正确地路由了身份验证请求,并按预期进行了身份验证。
不确定缓存是如何搞砸的,但我猜我的配置缓存卡在网络防护上,现在清除它可能会正确地通过 api 防护。
希望这对其他人有帮助。
我遇到了同样的问题。我正在使用 Laravel 护照。一切都与配置有关。到达 config/auth.php
并将 api 数组的驱动程序名称更改为守卫数组
中的护照
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
当你使用错误的授权中间件时也可能会发生:
public function __construct()
{
$this->middleware('auth:api');
}
例如使用 sanctum :
public function __construct()
{
$this->middleware('auth:sanctum');
}
我遇到了同样的问题,我已经通过
解决了这个问题
php artisan config:clear
php artisan config:cache
php artisan cache:clear
可能有帮助
尝试 运行:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
我已经通过 运行 composer window
中的以下命令解决了这个问题
- php artisan key:generate
- php artisan config:cache
- php artisan cache:clear
- php artisan config:clear
您的用户 table 中必须有一个名为 api_token 的列,
并且此列必须为每条记录填充唯一值,
你可以像
一样将它添加到你的迁移中
$table->string(‘api_token’, length)->after( .. ) 或手动创建
api 应用程序通过使用 api_token 值而不是“id”
对用户进行身份验证
我知道这个问题得到了回答,但是这个答案是为那些真正想深入了解错误真正原因的人解释的。
请记住,如果您在API中的路由是受保护的路由,并且您需要在验证后向受保护的路由发送请求时指定令牌作为承载,这是错误的。
以下路线受到保护,如果我想请求它,我将需要一个通过护照身份验证生成的访问令牌。
Route::middleware('auth:api')->group(function () {
Route::get('/loginCheck', 'Auth\ApiAuthController@LoginCheck');
});
将“token”更改为“passport”时需要做的是:
Location of below file: config\auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
然后您将需要运行以下命令。
php artisan cache:clear
php artisan route:clear
php artisan config:cache
现在再次启动 laravel 服务器
php artisan serve
大功告成!
我看过很多关于此错误的帖子,但是 none 的解决方案对我有用。
我 运行正在 Laravel 使用 Passport,它在我的开发服务器上运行良好。正如预期的那样,当尝试检查用户是否在我的开发服务器上经过身份验证时,它 returns 捕获(在 axios 调用中):
开发服务器
"message":"Unauthenticated."
但是,当 运行在我的生产服务器上使用相同的代码时,returns 问题:
生产服务器:
"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'api_token'
in 'where clause' (SQL: select * from `users` where `api_token` = ...
我有 运行 护照迁移并且 ['guards']['api']['driver'] 在 config/auth.[=48 中设置为护照=],并更新了显然为其他人解决了问题的配置缓存。
看起来身份验证需要使用护照迁移中的 oauth tables,但查询似乎是在查看用户 table。
编辑:
我能够确定我的开发服务器使用 RequestGuard
class 来查找用户,而我的生产服务器使用 TokenGuard
class 来查找用户。
尽管出现了令人困惑的错误,但我还是按照步骤
不确定缓存是如何搞砸的,但我猜我的配置缓存卡在网络防护上,现在清除它可能会正确地通过 api 防护。
希望这对其他人有帮助。
我遇到了同样的问题。我正在使用 Laravel 护照。一切都与配置有关。到达 config/auth.php
并将 api 数组的驱动程序名称更改为守卫数组
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
当你使用错误的授权中间件时也可能会发生:
public function __construct()
{
$this->middleware('auth:api');
}
例如使用 sanctum :
public function __construct()
{
$this->middleware('auth:sanctum');
}
我遇到了同样的问题,我已经通过
解决了这个问题php artisan config:clear
php artisan config:cache
php artisan cache:clear
可能有帮助
尝试 运行:
php artisan config:cache
php artisan cache:clear
php artisan config:clear
我已经通过 运行 composer window
中的以下命令解决了这个问题- php artisan key:generate
- php artisan config:cache
- php artisan cache:clear
- php artisan config:clear
您的用户 table 中必须有一个名为 api_token 的列,
并且此列必须为每条记录填充唯一值,
你可以像
$table->string(‘api_token’, length)->after( .. ) 或手动创建
api 应用程序通过使用 api_token 值而不是“id”
对用户进行身份验证我知道这个问题得到了回答,但是这个答案是为那些真正想深入了解错误真正原因的人解释的。
请记住,如果您在API中的路由是受保护的路由,并且您需要在验证后向受保护的路由发送请求时指定令牌作为承载,这是错误的。 以下路线受到保护,如果我想请求它,我将需要一个通过护照身份验证生成的访问令牌。
Route::middleware('auth:api')->group(function () {
Route::get('/loginCheck', 'Auth\ApiAuthController@LoginCheck');
});
将“token”更改为“passport”时需要做的是:
Location of below file: config\auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',//instead of token
'provider' => 'users',
'hash' => false,
],
],
然后您将需要运行以下命令。
php artisan cache:clear
php artisan route:clear
php artisan config:cache
现在再次启动 laravel 服务器
php artisan serve
大功告成!