laravel - 如何在 laravel 中获取最近 30 天的数据?
问题描述
我正在查询过去 30 天的数据,但没有得到预期的结果。今天是 2021 年 1 月 18 日,最后三天的日期是 2021 年 1 月 18 日,但我只得到这个月份和年份的数据,而不是 12 月我怎么能得到?我的代码是
$startDate = \Carbon\Carbon::now()->subDays(30);
$endDate = \Carbon\Carbon::now();
return Auth::user()->invoices()
->whereBetween("invoice_date", [$startDate, $endDate])
->orderBy('created_at', 'DESC')
->get();
并尝试其他类似的解决方案,
$date = \Carbon\Carbon::now()->subDays(30);
return Auth::user()->invoices()
->whereDate("invoice_date", '>=', $date)
->orderBy('created_at', 'DESC')
->get();
得到相同的输出我的数据库表数据是,
并显示出来是
解决方案
试试下面的代码,它会帮助你:
$date = \Carbon\Carbon::now()->subDays(30);
$year = \Carbon\Carbon::now()->year;
return Auth::user()->invoices()
->where("invoice_date", '>=', $date)
->where("invoice_date", '>=', $year)
->orderBy('created_at', 'DESC')
->get();
推荐阅读
- python - 运行 Python 程序时出错(新手)
- python - django,以可视形式将对象从数据库获取到模板
- java - log4j 文件随机进入桌面
- javascript - 使用 USB 数据线从 Beacon 读取/写入数据
- android - 如何在启动另一个设备之前等待设备完成最后一个 adb 命令?
- php - PHP在两个字符串之间获取字符串
- ios - 按单词包装 UILabel 并以编程方式扩展 UITableViewCell 高度
- html - 位置:绝对;仅在将鼠标悬停在 div 上时可见
- ms-access - 如何在以查询为控制源的报表上显示有关 n 个项目的信息(从另一个表中获取)?
- javascript - 如何在 JQuery 中创建一个全局函数?