简体   繁体   English

Laravel - 认证返回“where子句中的列'id'是不明确的”因为GlobalScope中的JOIN

[英]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.

相关问题 内部连接错误 - where 子句中的列“id”不明确 - Inner join error - Column 'id' in where clause is ambiguous Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel 在 where 子句中加入两个表列 id 不明确 - Laravel joining two tables column id in where clause ambiguous Laravel Nova列中where子句不明确/ where子句中的未知列 - Laravel Nova Column in where clause is ambiguous/Unknown column in where clause 对于3个表,where子句中的“ id”列含糊不清 - Column 'id' in where clause is ambiguous for 3 tables Codeigniter where子句中的列'id_f'是不明确的 - Column 'id_f' in where clause is ambiguous Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 违反完整性约束:1052 where子句不明确的列'prof_id'Laravel - Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM