我正在尝试使用 laravel 检查两个日期之间从开始时间到结束时间的房间预订

I am trying to check the room reservation from start time to end time between two dates using laravel

我正在为一家小旅馆开发 reservation/booking 系统 我对 PHP/laravel 很满意,但对 MySQL 不太好 我制作了一个表格,您可以在其中输入你的信息,start_date ,end_date ,start_time ,end_time ,但我想在 booking.and 之前检查房间预订我有点困惑我的逻辑是正确的还是不请帮我谢谢。

预订控制器

public function getBooking(Request $request){

 $startDateTime = date('Y-m-d H:i:s', strtotime("$request->start_date, 
    $request->start_time"));

    $endDateTime = date('Y-m-d H:i:s', strtotime("$request->end_date, 
     $request->end_time"));

    $isBooked = Booking::where('room_id', $request->roomId)
    ->whereDate('start_datetime', '<=', $startDateTime)
    ->whereDate('end_datetime', '>=', $endDateTime )
    ->exists();

    if(!$isBooked){
        return response()->json(['success'=>'Slot Available!']);
    }else{

        return response()->json(['errorMessage'=>'Slot Not Available']);
    }

    }

我发现您的预订模型存在设计问题,因为您将日期和时间存储在不同的列中,因此很难查询您正在检查的结果。

最好存储 start_datetime 和 end_datime,这样您的查询会更清晰。

在 mysql 中,您可以像这样将日期和时间转换为时间戳

ADDTIME(CONVERT(date, DATETIME), time)

但我怀疑它是否适用于 eloquent