bookings() ->occupying() ->overlapping($from, $until) ->when($ignore, fn ($query) => $query->whereKeyNot($ignore->getKey())) ->exists(); return ! $bookingConflict && ! $property->blockedPeriods()->occupying()->overlapping($from, $until)->exists(); } /** * Occupied nights within [$from, $until) as a set of 'Y-m-d' strings. * * @return array */ public function occupiedNights(Property $property, CarbonImmutable $from, CarbonImmutable $until): array { $periods = $property->bookings()->occupying()->overlapping($from, $until)->get(['arrival', 'departure']) ->map(fn (Booking $booking): array => [$booking->arrival, $booking->departure]) ->concat($property->blockedPeriods()->occupying()->overlapping($from, $until)->get(['starts_on', 'ends_on']) ->map(fn (BlockedPeriod $period): array => [$period->starts_on, $period->ends_on])); $nights = []; foreach ($periods as [$start, $end]) { for ($night = $start->max($from); $night->lessThan($end) && $night->lessThan($until); $night = $night->addDay()) { $nights[$night->toDateString()] = true; } } return $nights; } }