2013-05-26 23 views
1

因此得到一个总和()/ COUNT()是在Laravel很容易... 但我会怎么看待过去的一个月里,每天让行的总和?获取每日汇总/总和日在Laravel排序

EG ...按创建日期分组。

所以我要回如3,2,4,5 含义3排在了今天的日期,2行昨日,4行的前一天创建的个性化......等

怎么办这在Laravel很容易? 当我使用created_at组时,它总是只返回1.

任何人都知道该怎么做?

感谢

+0

我会说,我已经想通了,使用laravel原材料查询,但只是想知道是否有执行这样的查询流利/雄辩的语言 – KyleK

+0

我提供完全相同的回答另一篇文章: http://stackoverflow.com/questions/15437854/laravel-multiple-count-queries-using-eloquent/15438413#15438413 – vFragosop

回答

7

我提供了相同的答案on another post。缩短它:

$date = new DateTime('tomorrow -1 month'); 

// lists() does not accept raw queries, 
// so you have to specify the SELECT clause 
$days = Object::select(array(
     DB::raw('DATE(`created_at`) as `date`'), 
     DB::raw('COUNT(*) as `count`') 
    )) 
    ->where('created_at', '>', $date) 
    ->group_by('date') 
    ->order_by('date', 'DESC') 
    ->lists('count', 'date'); 

// Notice lists returns an associative array with its second and 
// optional param as the key, and the first param as the value 
foreach ($days as $date => $count) { 
    print($date . ' - ' . $count); 
} 
+0

谢谢,我解决了它反正....但是这是不是更优雅我做了什么 – KyleK

+0

谢谢。我不得不使用'groupBy'和'orderBy'来代替'group_by''order_by'。你的回答很有帮助。 – Neel

+0

作为魔术师工作 –