Eloquent中嵌套的“AND OR”查询

时间:2014-08-04 23:35:22

标签: laravel laravel-4 eloquent

我目前正在尝试创建嵌套查询,如下所示:

public function getChallenge($user_id, $opponent_id)
{
    $challenge = $this->challenges()
            ->where('open', true)
            ->where(function($query) use ($user_id, $opponent_id) {
                    $query->where('player_1', $user_id)
                          ->where('player_2', $opponent_id);
                })
                 ->orWhere(function($query) use ($opponent_id, $user_id) {
                    $query->where('player_1', $opponent_id)
                          ->where('player_2', $user_id);
                })
            ->first();

    return $challenge;
}

这会创建以下查询,例如:

select * from `site_challenges_leagues` 
where `site_challenges_leagues`.`league_id` = '1' 
and `open` = '1'
and (`player_1` = '3' and `player_2` = '1') 
or (`player_1` = '1' and `player_2` = '3') 
limit 1

但是,这始终会返回表格中的第一个值(其中open10),这是不正确的。为了使查询正确,它需要在括号中包含两组AND个查询,如下所示:

 select * from `site_challenges_leagues` 
 where `site_challenges_leagues`.`league_id` = '1' 
 and `open` = TRUE 

 and ((`player_1` = '3' and `player_2` = '1') 
 or (`player_1` = '1' and `player_2` = '3'))

 limit 1

是否可以在Laravel中执行此操作?我试图这样做;然而,它失败了:

public function getChallenge($user_id, $opponent_id)
{
    $challenge = $this->challenges()
            ->where('open', true)
            ->where(function($q) use ($user_id, $opponent_id) {
                $q->where(function($query) {
                        $query->where('player_1', $user_id)
                              ->where('player_2', $opponent_id);
                    })
                  ->orWhere(function($query) {
                        $query->where('player_1', $opponent_id)
                              ->where('player_2', $user_id);
                    })
                })
            ->first();

    return $challenge;
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:29)

你非常接近答案

$challenge = $this->challenges()
        ->where('open', true)
        ->where(function($q) use ($user_id, $opponent_id) {
            $q->where(function($query) use ($opponent_id, $user_id){
                    $query->where('player_1', $user_id)
                          ->where('player_2', $opponent_id);
                })
              ->orWhere(function($query) use ($opponent_id, $user_id) {
                    $query->where('player_1', $opponent_id)
                          ->where('player_2', $user_id);
                });
            })
        ->first();

Here是两个代码之间的差异

答案 1 :(得分:1)

我需要嵌套的位置,可以通过多个字段来搜索用户:

    $users_query->where('company_uuid', '=', $company_uuid);
    $users_query->where('is_active', '=', true);
    $users_query->whereNested(function($query) use ($search){
        /** @var Builder $query */
        $query
            ->where('first_name', 'like', '%' . $search . '%')
            ->orWhere('last_name', 'like', '%' . $search . '%')
            ->orWhere('email', 'like', '%' . $search . '%');
    });

假设您要堆叠where层而不会因使用orWhere而丢失 这不是一回事吗?

   $challenge = $this->challenges()
       ->where('open', true)
       ->whereNested(function($q) use ($user_id, $opponent_id) {
           $query->where('player_1', $user_id)
               ->where('player_2', $opponent_id);

           $query->orWhere('player_1', $opponent_id)
               ->where('player_2', $user_id);
   })->first();