Php Laravel-身份验证返回”;列';id';在where子句中是不明确的;因为加入了全球视野
我在Laravel认证方面遇到了麻烦。当我登录时,Laravel返回错误:“where子句中的'id'列不明确”,因为我有一个具有联接的GlobalScope 错误:Php Laravel-身份验证返回”;列';id';在where子句中是不明确的;因为加入了全球视野,php,laravel,authentication,Php,Laravel,Authentication,我在Laravel认证方面遇到了麻烦。当我登录时,Laravel返回错误:“where子句中的'id'列不明确”,因为我有一个具有联接的GlobalScope 错误: Column 'id' in where clause is ambiguous (SQL: select * from `users` inner join `playables` as `p` on `users`.`id` = `p`.`user_id` inner join `league_of_legends` as `
Column 'id' in where clause is ambiguous (SQL: select * from `users` inner join `playables` as `p` on `users`.`id` = `p`.`user_id` inner join `league_of_legends` as `lol` on `p`.`playable_id` = `lol`.`id` and `p`.`playable_type` like '%LeagueOfLegends%' where `id` = 1 and `users`.`deleted_at` is null and `users`.`banned` = 0 limit 1)
登录代码:
Auth::login($user);
全球范围:
$builder->join('playables as p', 'users.id', '=', 'p.user_id')
->join('league_of_legends as lol', function ($join) use ($game){
$join->on( 'p.playable_id', '=', 'lol.id');
$join->on('p.playable_type', 'like', DB::raw( "'%$game%'"));
});
我试图重命名模型用户的主键,但这导致了其他错误。还有别的选择吗?或者我必须使用本地范围
谢谢。
Builder
将它的where
子句作为$Builder->where
公开存储在数组中
因此,您可以访问和修改变量。但这一次只是因为这显然不是正确的方法
当前的$builder->where
如下所示
array(3) {
[0]=>
array(5) {
["type"]=> string(5) "Basic"
["column"]=> string(2) "id"
["operator"]=> string(1) "="
["value"]=> string(1) "1"
["boolean"]=> string(3) "and"
}
// ... Others are array of column deleted_at and banned
}
所以只需循环$builder->where
,并将其修改为
foreach( $builder->wheres as $key => $item ){
// Only modify column of 'id'
if( $item['column'] == 'id' ){
$builder->wheres[$key]['column'] = 'users.id';
break;
}
}
您可以将第二个join
子句放置为where
子句
$builder->join( 'league_of_legends AS lol', function( $join ){
$join->on( 'p.playable_id', '=', 'lol.id');
})->where( 'p.playable_type', 'LIKE', DB::raw( "'%$game%'") );
最后,为用户选择
// This also reset the SQL SELECT that previously defined
$builder->select( 'users.* AS users' );
就这样吧
foreach( $builder->wheres as $key => $item ){
// Only modify column of 'id'
if( $item['column'] == 'id' ){
$builder->wheres[$key]['column'] = 'users.id';
break;
}
}
$builder
->select( 'users.* AS users' )
->join( 'playables AS p', 'users.id', '=', 'p.user_id' )
->join( 'league_of_legends AS lol', function( $join ){
$join->on( 'p.playable_id', '=', 'lol.id');
})->where( 'p.playable_type', 'LIKE', DB::raw( "'%$game%'") );
如果您使用的是像我这样的雄辩生成器,解决方案是在具有全局范围的模型上指定主键名称:
protected $primaryKey = "users.id";
您是否在您的users
模型中尝试过使用$primaryKey='users.id'
?是的,我尝试过。我是如何回答这个问题的:“我试图重命名model User的主键,但这导致了其他错误”。:/@RegisZanandrea将$primaryKey='users.id'
添加到您的用户模型不会影响结果查询?这与重命名列本身不同。这是可行的,但会导致其他错误。与User::find(2)类似:“未找到列users.User.id”。