Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
Laravel: how to mix 'and' and 'or' conditions in a where clause?

I've searched and found similar posts but none of them are the same as my case. Seems the Laravel documents don't show a case like mine though my case is quite common in a where clause of database queries.

The where clause in my case is like the following and I can't get it to work with where() and orwhere() methods and tricks people suggested in other posts. Any info appreciated. Thanks.

where a = 1 and b = 2 and (c = 3 or c = 4) and (d = 5 or d = 6)

Another level of complication is that I can combine all 'and' conditions into one single array and feed it into one where() method, but I have to group 'or' conditions into separate arrays and how many groups are dynamically collected in the codes, such as the following.

$and = [['a', '=', 1], ['b', '=', 2], ...];

if($cond1) $or['or1'] = [['c', '=', 3], ['c', '=', 4], ...];
if($cond2) $or['or2'] = [['d', '=', 5], ['d', '=', 6], ...];
if($cond3) $or['or3'] = [['e', '=', 7], ['e', '=', 8], ...];
...

PS: I like to thank all who replied (posts or comments). I've seriously considered all suggestions and tried some that could be applied to my case, but unfortunately none of them worked. So my solution is just to compile a raw where clause as shown in the example I gave above and used whereRaw() method. It's straight forward to do and worked quite well.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can do it like so

Model::where('a', '1')
    ->where('b', '2')
    ->where(function ($q) {
         $q->where('c', '3')->orWhere('c', '4');
    })
    ->where(function ($q) {
         $q->where('d', '5')->orWhere('d', '5');
    })->get();
over 4 years ago · Santiago Trujillo Report

0

Something like this should do the trick:

Model::where([
    ['a', '=', 1],
    ['b', '=', 2],
    [['c', '=', 3], 'OR', ['c', '=', 4]],
    [['d', '=', 5], 'OR', ['d', '=', 6]]
]);
over 4 years ago · Santiago Trujillo Report

0

If I got your question, then this receipe is for you :

$result = DB::table('table')
   ->where('a', 1)
   ->where('b', 2)
   ->where(function($query) {
      $query->where('c', 3)
            ->orWhere('c', 4)
   })
   ->where(function($query) {
      $query->where('d', 5)
            ->orWhere('d', 6)
   ->get();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!