Laravel 4 Eloquent Query Using WHERE with OR AND OR?

Farzher picture Farzher · Jun 8, 2013 · Viewed 361.9k times · Source

How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)

For more complicated queries am I supposed to use raw SQL?

Answer

rmobis picture rmobis · Jun 8, 2013

Make use of Parameter Grouping (Laravel 4.2). For your example, it'd be something like this:

Model::where(function ($query) {
    $query->where('a', '=', 1)
          ->orWhere('b', '=', 1);
})->where(function ($query) {
    $query->where('c', '=', 1)
          ->orWhere('d', '=', 1);
});