Search code examples
phplaraveleloquentlaravel-query-builder

Laravel Eloquent Query: Using WHERE with OR AND OR?


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?


Solution

  • Make use of Logical Grouping (Laravel 7.x/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);
    });