Laravel:在给定的日期范围内,如何从表中为每个日期添加单独的价格?
我尝试动态计算房价。我的房价表目前有三个字段:Laravel:在给定的日期范围内,如何从表中为每个日期添加单独的价格?,laravel,eloquent,date-range,php-carbon,Laravel,Eloquent,Date Range,Php Carbon,我尝试动态计算房价。我的房价表目前有三个字段: id | room_rate | starting_from | ending_in 1 | 60 | 2019-12-01 | 2019-12-20 2 | 80 | 2019-12-21 | 2020-01-04 如果用户有入住和退房日期,我想查看每天的价格 我获得独立之夜的公共功能如下所示: $booking_date_start[] = $request->input('booking_d
id | room_rate | starting_from | ending_in
1 | 60 | 2019-12-01 | 2019-12-20
2 | 80 | 2019-12-21 | 2020-01-04
如果用户有入住和退房日期,我想查看每天的价格
我获得独立之夜的公共功能如下所示:
$booking_date_start[] = $request->input('booking_date_start');
$booking_date_end[] = $request->input('booking_date_end');
$booking_total = array();
foreach(array_combine($booking_date_start, $booking_date_end) as $check_in => $check_out) {
$check_in = new Carbon($check_in);
$check_out = new Carbon($check_out);
while ($check_in->lte($check_out)) {
$booking_total[] = $check_in->toDateString();
$check_in->addDay();
}
array_pop($booking_total);
}
return response()->json($booking_total);
["2019-12-20, 60","2019-12-21, 80","2019-12-22, 80"]
我的功能输出(签入:2019-12-20;签出:2019-12-23):
如何解析房价表并添加每个日期的独立价格
我需要这样的东西:
$booking_date_start[] = $request->input('booking_date_start');
$booking_date_end[] = $request->input('booking_date_end');
$booking_total = array();
foreach(array_combine($booking_date_start, $booking_date_end) as $check_in => $check_out) {
$check_in = new Carbon($check_in);
$check_out = new Carbon($check_out);
while ($check_in->lte($check_out)) {
$booking_total[] = $check_in->toDateString();
$check_in->addDay();
}
array_pop($booking_total);
}
return response()->json($booking_total);
["2019-12-20, 60","2019-12-21, 80","2019-12-22, 80"]
谢谢 我解决了这个问题。对于那些有兴趣的人 首先,我重构了我的房价表。对于每个日期,我在表中使用单独的条目和单独的价格(不再有日期范围) 第二,我过滤了我的房价表。我将
$booking\u total
值与我的数据库查询(其中方法)相结合,并将结果解码为JSON格式