Home > Software design >  I am trying to check the room reservation from start time to end time between two dates using larave
I am trying to check the room reservation from start time to end time between two dates using larave

Time:04-10

I'm working on a reservation/booking system for a small hotel i'm pretty good with PHP/laravel but not so good with MySQL I have made a form where you enter your information,start_date ,end_date ,start_time ,end_time ,but i am trying to check room reservation before booking.and i am little confused my logic is right or not please help me thank.

BookingController

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']);
    }

    }

enter image description here

enter image description here

CodePudding user response:

I see a design problem in your booking model, since you store the date and the hour into different columns it makes it hard to query for the result you are checking.

It's better if you store a start_datetime and end_datime so your query can be much cleaner.

In mysql you can concat convert date and time into timestamp like this

ADDTIME(CONVERT(date, DATETIME), time)

but I doubt that it works with eloquent

  • Related