Laravel 4 Eloquent Query gebruiken WHERE met OR AND OR?

Hoe zeg ik WAAR (a = 1 OF b =1 ) EN (c = 1 OF d = 1)

Moet ik voor meer ingewikkelde queries ruwe SQL gebruiken?

Oplossing

Maak gebruik van Parameter Grouping (Laravel 4.2). Voor jouw voorbeeld, zou het zoiets zijn als dit:

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

Als je parameters wilt gebruiken voor a,b,c,d in Laravel 4

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

Incase you're looping the OR conditions, you don't need the second $query->where from the other posts (actually I don't think you need in general, you can just use orWhere in the nested where if makkelijker)

$attributes = ['first'=>'a','second'=>'b'];

$query->where(function ($query) use ($attributes) 
{
    foreach ($attributes as $key=>value)
    {
        //you can use orWhere the first time, doesn't need to be ->where
        $query->orWhere($key,$value);
    }
});
Commentaren (1)