Come si dice WHERE (a=1 OR b=1) AND (c=1 OR d=1)
Laravel 4 eloquente DOVE CON OR E O?
Per query più complicate dovrei usare SQL raw?
Come si dice WHERE (a=1 OR b=1) AND (c=1 OR d=1)
Laravel 4 eloquente DOVE CON OR E O?
Per query più complicate dovrei usare SQL raw?
Utilizzare Parameter Grouping (Laravel 4.2). Per il vostro esempio, sarebbe qualcosa di simile:
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
Se si desidera utilizzare i parametri per 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);
});
Incase si sta avvolgendo il OR condizioni, non è necessario il secondo $ query-> dove dagli altri posti (in realtà non credo che avete bisogno, in generale, si può semplicemente utilizzare orWhere nel nidificato in cui se più facile)
$attributes = ['first'=>'a','second'=>'b'];
$query->where(function ($query) use ($attributes)
{
foreach ($attributes as $key=>value)
{
//you can use orWhere the first time, dosn't need to be ->where
$query->orWhere($key,$value);
}
});
Penso che dovrai aggiungere la parola chiave 'use' per utilizzare la variabile attributes all'interno di dove. –
È inoltre possibile utilizzare gli ambiti di query per rendere le cose a po 'più ordinato, in modo da poter fare qualcosa di simile:
Invoice::where('account', 27)->notPaidAt($date)->get();
Poi nel modello
public function scopeNotPaidAt($query, $asAt)
{
$query = $query->where(function ($query) use ($asAt) {
$query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt);
});
return $query;
}
È possibile anche interrogare il primo o condizione e poi si può applicare un altro o condizione
$model = Model::where('a',1)->orWhere('b',1);
ora applicare un'altra condizione su quella variabile $model
$model1 = $model->where('c',1)->orWhere('d',1)->get();
se si desidera utilizzare le parentesi in laravel 4 e non dimenticate tornare
In laravel 4 (almeno) è necessario utilizzare $ a, $ b tra parentesi come nell'esempio
$a = 1;
$b = 1;
$c = 1;
$d = 1;
Model::where(function ($query) use ($a, $b) {
return $query->where('a', '=', $a)
->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
return $query->where('c', '=', $c)
->orWhere('d', '=', $d);
});
Grazie per aver menzionato l'uso ($ a, $ b) – mentorgashi
$ a, $ b, $ c, $ d può essere valori dinamici dalla query
->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);
})
Basta usare in laravel eloquente:
$a='foo', $b='bar', $c='john', $d='doe';
Coder::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);
})->get();
produrrà una query come:
SELECT * FROM <table> WHERE (a='foo' or b='bar') AND (c='john' or d='doe');
Just do 'funzione ($ query) utilizzare ($ param1, $ param2) ...' ; Oh, hai cancellato il tuo commento. – rmobis
Il corretto collegamento a Laravel doco è: http://laravel.com/docs/queries#advanced-where-clauses – dave