Something like this.
Room::query()->whereRaw('id NOT IN (SELECT room_id FROM bookings WHERE ? BETWEEN arrival AND departure)', [$arrival])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, could someone kindly assist me to convert the following query to Eloquent, Kindly?
$availableRooms = DB::select("SELECT * FROM rooms WHERE id NOT IN (SELECT room_id FROM bookings WHERE '$arrival' BETWEEN arrival AND departure)");
@Brammah No. with() loads the relationship. It does not limit the rooms query in any way. To do that you can use whereDoesntHave()
Room::query()->whereDoesntHave('bookings', function($query) use ($arrival) {
$query->whereRaw('? BETWEEN arrival AND departure)', [$arrival]);
})->get();
or if you want both
Room::query()->whereDoesntHave('bookings', function($query) use ($arrival) {
$query->whereRaw('? BETWEEN arrival AND departure)', [$arrival]);
})->with(['bookings' => function($query) use ($arrival) {
$query->whereRaw('? BETWEEN arrival AND departure)', [$arrival]);
}])->get();
Please or to participate in this conversation.