Laravel:如何为给定日期范围内的每个日期添加 table 中的单独价格?

Laravel: How can I add an individual price from a table for each date within a given date range?

我尝试动态计算房价。我的 table 房价目前有三个字段:

id | room_rate | starting_from | ending_in
 1 |        60 | 2019-12-01    | 2019-12-20
 2 |        80 | 2019-12-21    | 2020-01-04

如果用户有入住和退房日期,我想查看每天的价格。

我的 public 获取个人夜晚的函数如下所示:

$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;签出:2019-12-23):

["2019-12-20","2019-12-21","2019-12-22"]

如何解析 table 房价并添加每个日期的个人价格?

我需要这样的东西:

["2019-12-20, 60","2019-12-21, 80","2019-12-22, 80"]

谢谢!

我解决了这个问题。对于那些有兴趣的人。

首先,我重构了 table 的房价。我在我的 table 中为每个日期使用一个单独的条目,其中包含单独的价格(不再有日期范围)。

其次,我过滤了 table 的房价。我将 $booking_total 值与我的数据库查询(whereIn 方法)结合起来,并将结果解码为 JSON 格式。