Nested 'AND OR' Query in Eloquent

后端 未结 2 1586
野趣味
野趣味 2020-12-14 07:49

I\'m currently attempting to create a nested query, as follows:

public function getChallenge($user_id, $opponent_id)
{
    $challenge = $this->challenges         


        
相关标签:
2条回答
  • 2020-12-14 08:14

    You were very close to the answer

    $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 are the differences between two codes

    0 讨论(0)
  • 2020-12-14 08:25

    I needed the nested wheres to search for a user by multiple fields:

        $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 . '%');
        });
    

    Supposing you want to stack where layers without losing them on using orWhere Wouldn't this be the same thing ?

       $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();
    
    0 讨论(0)
提交回复
热议问题