3
我想优化我的代码,其中我在过去七天生成统计信息。Laravel和使用雄辩的多重计数查询
目前我正在使用口才&查询每天的记录数据的计数,这使我在一个循环内进行7个单独的查询。
例如:
// loop for number of days
for($i = 0; $i < $days; $i++){
// some logic to set comparitive unix times
$oldest = $newest - $dayDuration;
// count number of objects between oldest time and newest time
$counts[$i] = Object::where('objecttime','>',$oldest)
->where('objecttime','<',$newest)->count();
// more logic to set comparitive unix times
$newest = $newest - $dayDuration;
}
我知道在SQL一个罐组查询中使用类似的语法描述here;我想知道的是,如果能够在Laravel中使用雄辩/流利来做同样的事情,还是只能使用原始查询来做到这一点?
编辑:我不知道我是否需要澄清,但这是一个Laravel 3的问题。
感谢您的回复,让我给一个bash并回报。 – twaambo 2013-03-15 18:19:05
我用最后使用的查询更新了答案。再次感谢Vinícius。 – twaambo 2013-03-22 09:00:11
group_by()和order_by()对我不起作用。我不得不使用groupBy()和orderBy()。 – 2014-10-03 15:11:24