Bis*_*Das 32 eloquent laravel-4
如何将以下查询转换为Laravel 4 eloquent ORM?
select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
Run Code Online (Sandbox Code Playgroud)
Qua*_*unk 73
像这样:
<?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();
Run Code Online (Sandbox Code Playgroud)
查看文档,了解使用Eloquent和Query Builder可以做的更酷的事情.
//编辑: 要将整个where子句包装在大括号中(就像在你的问题中一样),你可以这样做:
<?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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28345 次 |
| 最近记录: |