Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajoyish's avatar

How to check check-in and check-out dates in bookings table?

Dear artisans, I am building a booking system. I have stored the following row in my bookings table

{
  "id": "1",
   "check_in": "2022-12-13",
  "check_out": "2022-12-20",
  "user_id": 1
}

How can I check those check_in and check_out dates that do not exists in the columns and store those dates using Eloquent or Query Builder? Your help is much appreciated, thank you so much! 🙏🙏

0 likes
1 reply
rajoyish's avatar

I want to store a booking in different dates that are not stored in the table already. How I disallow a user to make booking with same room in the same check-in and check out dates that is already booked? In other words, a room can be booked only once with different dates. I don't want to overlaps booking with same room with dates.

Here' what I have done so far :

public function store(Request $request)
    {
        $request->validate([
            'check_in' => ['required', 'date'],
            'check_out' => ['required', 'date'],
            'amount' => ['numeric'],
        ]);

        if (DB::table('booking_room')->where('room_id', $request->room_id)->exists()) {
            return to_route('rooms')->with('error', 'Sorry! The room is already booked!');
        }

        if (Auth::user()->is_verified === false) {
            return back()->with('error', 'Sorry! Your account is not verified, please call us to do so.');
        }

        //        Days based on check in & check out
        $check_in = Carbon::parse($request->check_in);

        // abort if user check-ins in the past
        if ($check_in->lessThan(today())) {
            return back()->with('error', 'Sorry! you cannot check-in the past.');
        }

        $check_out = Carbon::parse($request->check_out);

        $days = $check_out->diffInDays($check_in);

        //        Rounding up to 1 if user chooses the same date
        $days = $days == 0 ? 1 : $days;

        //      Total price
        $room = Room::find($request->room_id);
        $price = $room->price;
        $total = $days * $price;

        //        Total amount with discount
        $discount_amount = \Auth::user()->discount->percentage;
        $amount = $total - ($total * ($discount_amount / 100));

        $booking = Booking::create([
            'check_in' => $check_in,
            'check_out' => $check_out,
            'amount' => $amount,
            'user_id' => \Auth::id(),
        ]);

        // booking_id
        Auth::user()->bookings();

        // room_id
        $booking->rooms()->attach($request->room_id, ['status' => (bool) false]);

        return to_route('user.dashboard')->with('success', 'Booked! Please pay the invoice to get confirmed.');
    }

I have done a mess here, I don't like this code at all and don't know how to write the logic:


 if (DB::table('booking_room')->where('room_id', $request->room_id)->exists()) {
            return to_route('rooms')->with('error', 'Sorry! The room is already booked!');
        }

Please or to participate in this conversation.