laravel - 使用 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 中它不能工作
解决方案
这个查询对我有用,最后我找到了解决方案。
$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');
});
推荐阅读
- r - 返回模拟定价结果
- raspberry-pi - Raspberry Pi 作为 WLAN 热点和 LAN 上的静态 IP
- javascript - 如何在 React JS 中使用三元运算符将 Font Awesome Icons 与文本一起使用?
- server - 在 2 个服务器之间自动同步和镜像数据 cPanel
- github - Github/Gitlab Repository 总分
- python - Python电报机器人:调用函数的InlineKeyboardButton
- spring - 我可以使用 EntityGraph 从实体中选择特定字段吗?
- python - OpenCV、Python:航拍图像拼接中的透视变形问题
- python - 根据 Elasticsearch 中的字段值对记录进行排名
- c++ - 我的 for 循环有更好更短的方法吗?