[英]Laravel - Authentication returns “Column 'id' in where clause is ambiguous” because a JOIN in a GlobalScope
I'm having a trouble with the Laravel Authentication. 我在使用Laravel身份验证时遇到了麻烦。 When I make a login, Laravel returns the error: "Column 'id' in where clause is ambiguous" because I have a GlobalScope that has a JOIN. 当我进行登录时,Laravel返回错误:“where子句中的列'id'是不明确的”,因为我有一个具有JOIN的GlobalScope。
The Error: 错误:
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)
Login Code: 登录代码:
Auth::login($user);
Global Scope: 全球范围:
$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%'"));
});
I tried to rename the primary key of model User but this cased others errors. 我试图重命名模型用户的主键,但这可以解决其他错误。 There is an alternative? 还有另一种选择吗? Or I have to use Local Scopes?? 或者我必须使用本地范围?
Thanks. 谢谢。
如果您像我一样使用Eloquent Builder,解决方案是在具有全局范围的模型上指定主键名称:
protected $primaryKey = "users.id";
Builder
store it's where
clauses publicly in array as $builder->wheres
. Builder
店是where
的条款公开在数组$builder->wheres
。
So you can access and modify the variable. 因此,您可以访问和修改变量。 But only for this time because it's not the correct way, obviously. 但这只是因为这不是正确的方式,显然。
The current $builder->wheres
is like this 目前的$builder->wheres
是这样的
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
}
So just loop the $builder->wheres
and modify it as 所以只需循环$builder->wheres
并将其修改为
foreach( $builder->wheres as $key => $item ){
// Only modify column of 'id'
if( $item['column'] == 'id' ){
$builder->wheres[$key]['column'] = 'users.id';
break;
}
}
You can put second join
clause as where
clause 您可以将第二个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%'") );
Finally do select
for users 最后select
用户
// This also reset the SQL SELECT that previously defined
$builder->select( 'users.* AS users' );
So it will be 所以它会
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%'") );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.