Sredny M Casanova
Sredny M Casanova

Reputation: 5063

how to handle Multiple Orwhere in Laravel

I'm working in Laravel 5 and am using the Orwhere clausule, my code to consult is this:

$estudiantes= Usuario::
        where('rol', '=', 'ESTUDIANTE')
            ->whereHas('perfil', function($query) use ($data){
                if($data!="")
                    $query->Where('doc_identidad', '=' ,$data)
                        ->orWhere('nombres','like','%'.$data.'%')
                        ->orWhere('apellidos','like','%'.$data.'%');
                dd($query);
            })->get();

So, I want to filter every Usuario with rol ESTUDIANTE that works very fine, then I want to filter which of these have doc_identidad or nombres or apellidos like you see, I have a dd($query) to see the tree of the query, and I noticed that the first where uses and instead of or. This is the tree:enter image description here

So, I have the next: where1 and where2 or where3 or where4

and I want: where1 and (where2 or where3 or where4) so, how should I do it? thanks!

Upvotes: 3

Views: 1509

Answers (1)

fico7489
fico7489

Reputation: 8560

where1(a=1) and (where2(b=1) or where3(c=1) or where4(d=1))

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

Upvotes: 2

Related Questions