首页 > 解决方案 > 使用 Laravel 获取最近 7 天的数据

问题描述

这是查询代码

  $day7 = transaction::count('id')->whereDate('created_at', '<=', \Carbon\Carbon::today()->toDateString())
   ->whereDate('created_at', '>=', \Carbon\Carbon::today()->subDays(7)->toDateString())->where([['merchant_id' ,'=',$merchant_id]])->groupBy('created_date')->get();

当我使用 get 的 count() 方法时,它会计算所有事务,但在 get 中它不能工作

标签: laraveleloquent

解决方案


这个查询对我有用,最后我找到了解决方案。

 $day7 =transaction::where('created_at', '<=', \Carbon\Carbon::now())

   ->where('created_at', '>=', \Carbon\Carbon::now()->subDays(6))->where([['merchant_id' ,'=',$merchant_id]])->get()->groupBy(function($item) {
     return $item->created_at->format('Y-m-d');
   });

推荐阅读