Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda