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

问题描述 投票:0回答:1

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

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

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

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

我需要这样的东西:

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

谢谢!

laravel eloquent date-range php-carbon
1个回答
0
投票

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

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

第二,我过滤了我的房价表。我将$booking_total值与数据库查询(whereIn方法)结合在一起,并以JSON格式解码结果。

© www.soinside.com 2019 - 2024. All rights reserved.