Laravel 4 Eloquent Query Using WHERE with OR AND OR?

Como dizer AQUI (a = 1 OU b =1 ) E (c = 1 OU d = 1)

Para consultas mais complicadas, devo usar o SQL bruto?

Solução

Faça uso de Agrupamento de Parâmetros (Laravel 4.2). Para seu exemplo, it'seria algo parecido com isto:

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

Se você quiser usar parâmetros para a,b,c,d em 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);
});
Comentários (0)

Incase you're loopinging 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 or Where in the nested where if easier)

$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);
    }
});
Comentários (1)