laravel-4 - update - laravel pagination
Laravel elocuente grupo de ORM donde (1)
¿Cómo convierto la siguiente consulta a Laravel 4 ORM elocuente?
select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
Me gusta esto:
<?php
$results = DB::table(''table'')
->where(function($query) use ($starttime,$endtime){
$query->where(''starttime'', ''<='', $starttime);
$query->where(''endtime'', ''>='', $endtime);
})
->orWhere(function($query) use ($otherStarttime,$otherEndtime){
$query->where(''starttime'', ''<='', $otherStarttime);
$query->where(''endtime'', ''>='', $otherEndtime);
})
->orWhere(function($query) use ($anotherStarttime,$anotherEndtime){
$query->where(''starttime'', ''>='', $anotherStarttime);
$query->where(''endtime'', ''<='', $anotherEndtime);
})
->get();
Eche un vistazo a la documentation para ver más cosas interesantes que puede hacer con Eloquent y el Generador de consultas .
// Editar: Para ajustar incluso la cláusula where completa entre llaves (como en su pregunta), puede hacer esto:
<?php
$results = DB::table(''table'')
//this wraps the whole statement in ()
->where(function($query) use ($starttime,$endtime, $otherStarttime,$otherEndtime, $anotherStarttime,$anotherEndtime){
$query->where(function($query) use ($starttime,$endtime){
$query->where(''starttime'', ''<='', $starttime);
$query->where(''endtime'', ''>='', $endtime);
});
$query->orWhere(function($query) use ($otherStarttime,$otherEndtime){
$query->where(''starttime'', ''<='', $otherStarttime);
$query->where(''endtime'', ''>='', $otherEndtime);
});
$query->orWhere(function($query) use ($anotherStarttime,$anotherEndtime){
$query->where(''starttime'', ''>='', $anotherStarttime);
$query->where(''endtime'', ''<='', $anotherEndtime);
});
})
->get();