Piz Piz - 3 months ago 13
PHP Question

Laravel 4 where with Between OR Between?

How do

whereBetween 'field1' val1 AND val2 OR whereBetween 'field2' val1 AND val2

...too hard to understand

enter link description here

Answer

Example: find users with ids between 1-10 OR 100-110:

$val1 = 1;
$val2 = 10;
$val3 = 100;
$val4 = 110;

$user = User::where(function($q) use ($val1, $val2)  {
    $q->whereBetween('id', [$val1, $val2]);
})->orWhere(function($q) use ($val3, $val4) {
    $q->whereBetween('id', [$val3, $val4]);
});
$user->toSql();

results in:

select * from `users` where (`id` between ? and ?) or (`id` between ? and ?)`

Not sure if that's what you're looking for.

Comments