laravel - 在 Laravel 中获取数据库中的所有记录到特定月份
问题描述
在我的数据库中,我有这个预订记录
App\Reservation {#2632
id: 1,
user_id: 7,
rest_id: 23,
reservation_date: "2019-08-29",
from_time: "16:00:00",
to_time: "00:00:00",
count_of_people: 15,
loyalty_points: 100,
confirme: 2,
cancle: 0,
surveySended: 0,
surveyAnswer: 0,
created_at: null,
updated_at: null,
},
在我的控制器中,我有这段代码试图检索本月的所有预订:
public function getRestAdmin(Request $request,$rest_id)
{
$restaurant=Restaurant::find($rest_id);
$today=\Carbon\Carbon::today();
//reservations made this month
$reservations=$restaurant->reservations()->where('confirme',2)->whereBetween('reservation_date',[$today->startOfMonth(),$today->endOfMonth()])->get();
}
但我总是无效!
解决方案
假设您在数据库中有这些日期的预订,请尝试让 Carbon 在直接查询之前完成繁重的工作(无需先设置为今天):
$start = new Carbon('first day of this month');
$end = new Carbon('last day of this month');
然后只需将变量添加到查询中:
whereBetween('reservation_date',[$start, $end]) ...
您也可以在 Carbon 上使用此方法:
$start = Carbon::now()->startOfMonth();
推荐阅读
- javascript - 如何识别嵌套对象中的键和过滤器字符串
- amazon-web-services - 如果 LB 中没有添加 ec2,有什么方法可以在负载均衡器上设置维护页面
- html - 编译错误:BC30807:不支持赋值语句“Let”和“Set”
- javascript - React Hooks:在“位置”中获取数据,但不是在“链接”的参数中
- ssas - SSAS 多维数据集 - 有多少“节点”?
- html - 移动设备上的网站博客页面 2 侧边栏问题
- reactjs - 我不知道如何最好地将新数据放入reducer
- python - 需要逐行读取大txt文件的功能,如果没有更多行,则打开文件并再次迭代
- excel - 带有表引用的 Excel 条件下拉列表
- arrays - 我可以使用 switch 语句来转换 Swift 中数组中的每个值吗?