1

所以在 Laravel 中得到 sum()/count() 真的很容易......但是我如何查看过去的一个月,并每天得到行的总和?

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

所以我想返回一个计数,例如 3、2、4、5 表示今天创建了 3 行,昨天创建了 2 行,前一天创建了 4 行......等等

如何在 Laravel 中轻松做到这一点?当我通过 created_at 使用组时,它总是只返回 1。

有人知道怎么做吗?

谢谢

4

1 回答 1

9

在另一篇文章中提供了相同的答案。缩短它:

$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);
}
于 2013-05-27T19:15:06.267 回答