分析Laravel中的每日统计数据

时间:2017-04-17 11:59:02

标签: php mysql pagination laravel-5.2

我在RAW

中有Laravel个查询
$sales = DB::table('orders')
             ->select(DB::raw('DATE(created_at) as date'), 
                      DB::raw('SUM(total_price) AS total_price'), 
                      'created_at')
             ->groupBy('date')
             ->orderBy('date', 'DESC')
             ->paginate(20);

我正在尝试对结果集进行分页,但它向我显示以下错误;

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date' in 'group statement' 
(SQL: select count(*) as aggregate from `orders` group by `date`)

上面的查询工作正常,并在我替换时返回完整的结果集;

带有paginate(20)

get()

发生了什么事?

1 个答案:

答案 0 :(得分:0)

$sales = DB::table('orders')
             ->select(DB::raw('DATE(created_at) as `date`'), 
                      DB::raw('SUM(total_price) AS total_price'), 
                      'created_at')
             ->groupBy('date')
             ->orderBy('date', 'DESC')
             ->paginate(20);

尝试使用此代码,它应该可以正常工作。