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

FareedR's avatar

Achieve available venue on that date.

i try to achieve "mini stadium" but collection still appear "stadium" and "mini stadium". should i use first() or get() ? or my query are wrong ? anyone can show me ?

database 
Venue
id | name |
1   stadium
2   mini stadium

Venue_has_bookings
id | venue_id | start_date | end_date|
1   1          2019-04-21   2019-04-25

controller
$startDate = 2019-04-21;
        $endDate = 2019-04-25;
        $availableVenue = Venue::with(['bookings' => function ($query) use ($startDate,$endDate){
            $query->where('endDate','>',$startDate)->where('startDate','>',$endDate)
            ->orWhere('endDate','<',$startDate)->where('startDate','<',$endDate);
        }])->get();
        dd($availableVenue);
0 likes
1 reply
Braunson's avatar

Hmm you are checking for Venues with bookings between your start and end date.

What about using whereNotBetween

$availableVenue = Venue::with(['bookings' => function ($query) use ($startDate, $endDate) {
    $query->whereNotBetween('startDate', [$startDate, $endDate])
            ->whereNotBetween('endDate', [$startDate, $endDate]);
}])->get();

dd($availableVenue);

There's likely a more ideal way to do this but it should work...

Please or to participate in this conversation.