2013-05-26 38 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

作爲魔術師工作 –