How to Create Multiple Where Clause Query Using Laravel Eloquent?

前端 未结 24 1372
一整个雨季
一整个雨季 2020-11-22 14:30

I\'m using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it\'s not elegant.

E

24条回答
  •  一生所求
    2020-11-22 15:19

    if your conditionals are like that (matching a single value), a simple more elegant way would be:

    $results = User::where([
             'this' => value,
             'that' => value,
             'this_too' => value,
              ...
          ])
        ->get();
    

    but if you need to OR the clauses then make sure for each orWhere() clause you repeat the must meet conditionals.

        $player = Player::where([
                'name' => $name,
                'team_id' => $team_id
            ])
            ->orWhere([
                ['nickname', $nickname],
                ['team_id', $team_id]
            ])
    

提交回复
热议问题