首页 > 解决方案 > 这是获取 Laravel 中可预订房间列表的功能。谁能帮我弄清楚查询有什么问题?

问题描述

2021-09-29 09:00 至 2021-09-29 10:00 预订房间时,房间应在 2021-09-29 10:00 开始预订,但在这种情况下,房间可用从 2021 年 9 月 29 日 10:01 开始。

public function searchRoom(Request $request)
{
    $rooms = null;
    if($request->filled(['start_time', 'end_time', 'capacity'])) {
        $times = [
            Carbon::parse($request->input('start_time')),
            Carbon::parse($request->input('end_time')),
        ];

        $rooms = Room::where('capacity', '>=', $request->input('capacity'))
            ->whereDoesntHave('events', function ($query) use ($times) {
                $query->whereBetween('start_time', $times)
                    ->orWhereBetween('end_time', $times)
                    ->orWhere(function ($query) use ($times) {
                        $query->where('start_time', '<', $times[0])
                            ->where('end_time', '>', $times[1]);
                    });
            })
            ->get();
    }

    return view('admin.bookings.search', compact('rooms'));
}

标签: phpmysqllaraveleloquentlaravel-8

解决方案


推荐阅读