Thibaud Lacan
Thibaud Lacan

Reputation: 356

Laravel Eloquent date range between range

I am building a website where i have a booking mechanism. The user can book a hotel room for X days if the room is available.

I am using:

The room is unavailable if:

If have 3 tables to store those data:

I am struggling to build a Eloquent query to check if the room is available before processing the user payment. Here is what i have for now:

public function isAvailableAtDates($rentalId, $startDate, $endDate, $capacity) {
    $from = min($startDate, $endDate);
    $till = max($startDate, $endDate);

    $result = DB::table('rentals')
        ->where([
            ['rentals.rentalId', '=', $rentalId],
            ['rentals.rentalCapacity', '>=', $capacity]
        ])
        ->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
            $query->select('unavailabilities.rentalId')
                ->from('unavailabilities')
                ->where([
                    ['unavailabilities.rentalId', '=', $rentalId],
                    ['unavailabilityStartDate', '>=', $from],
                    ['unavailabilityEndDate', '<=', $till],
                ]);

        })
        ->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
            $query->select('rent.rentalId')
                ->from('rent')
                ->where([
                    ['rent.rentalId', '=', $rentalId],
                    ['rentStartDate', '>=', $from],
                    ['rentEndDate', '<=', $till]
                ]);
        })
        ->select('rentals.rentalId')
        ->get();

    return count($result) == 1;
}

Let's say I have a row inside Unavailabilities with:

When calling the method with some dates outside of the range stored in Unavailabilities, i'm getting the expected result. When calling it with the exact same dates, i'm getting no result (which is what i want). So far so good! The problem is if i'm calling it with a start date between 26 of April and 30th and a end date later in May, i am still getting a result even tho i shouldn't.

Could anyone help me with that?

Upvotes: 0

Views: 3049

Answers (1)

skido
skido

Reputation: 462

This is not a laravel, nor a mysql issue.

try this:

->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
        $query->select('unavailabilities.rentalId')
            ->from('unavailabilities')
            ->where([
                ['unavailabilities.rentalId', '=', $rentalId],
                ['unavailabilityStartDate', '<=', $till],
                ['unavailabilityEndDate', '>=', $from],
            ]);

    })
->whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
        $query->select('rent.rentalId')
            ->from('rent')
            ->where([
                ['rent.rentalId', '=', $rentalId],
                ['rentStartDate', '<=', $till],
                ['rentEndDate', '>=', $from]
            ]);
    })

You need all rent and unavailabilities records that had been started before $till and hadn't been ended before $from date.

Try to draw a time diagram.

Upvotes: 1

Related Questions