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

KalimeroMK's avatar

Help with qery builder

Need to list all vehicles form vehicles table who are available for given start_date and end_date but the date are in vehicle_rental_times table not in vehicles table so if vehicles is rent and start and end date is the same as give date i the src parameter to be remove not shown in the list or the time is not the same to be show in the list

my code

                $query = Vehicle::query();
 $query->whereDoesntHave('vehicle_rental_times')
                            ->orWhereHas('vehicle_rental_times', function ($q) use ($start_datetime, $end_datetime) {
                                return $q->orWhere('booking_end_datetime', '<', $start_datetime)
                                         ->orWhere('booking_start_datetime', '>', $end_datetime);
                            }
                            );

relation in model Vehicle

 /**
         * @return HasMany
         */
        public function vehicle_rental_times(): HasMany
        {
            return $this->hasMany(VehicleRentalTime::class);
        }
0 likes
7 replies
fylzero's avatar

@kalimeromk I'm not super clear on what you're trying to accomplish... but maybe this or looking into using when() in Eloquent might help guide you...

$vehicles = Vehicle::query();
$vehicles->whereDoesntHave('vehicle_rental_times')
    ->orWhereHas('vehicle_rental_times', function ($query) use ($start_datetime, $end_datetime) {
        if ($start_datetime != $end_datetime) {
            return $query->orWhere('booking_end_datetime', '<', $start_datetime)
                ->orWhere('booking_start_datetime', '>', $end_datetime);
        } else {
            // Something Else
        }
    }
);
KalimeroMK's avatar

@fylzero to be clear for an example: Vehicle with id 10 is rent it will show in vehicle_rental_times with star and end time what I need is away to show Vehicle with id 10 every time except when is ask for period when is already rent

KalimeroMK's avatar

@fylzero

this is my SQL it working nice need to translate to query builder

SELECT *
FROM vehicles AS r
WHERE r.id NOT IN(
    SELECT m.vehicle_id
    FROM vehicle_rentals AS b
             JOIN vehicle_rental_times AS m
                  ON b.vehicle_id = m.vehicle_id
    WHERE m.booking_start_datetime < '2022-06-07 17:00:00'
      AND m.booking_end_datetime >   '2022-06-07 15:00:00'
fylzero's avatar
fylzero
Best Answer
Level 67

@KalimeroMK

DB::table('vehicles as r')
  ->whereNotIn('r.id', function ($query) {
    return $query->select('m.vehicle_id')->from('vehicle_rentals as b')
      ->join('vehicle_rental_times as m', 'b.vehicle_id', '=', 'm.vehicle_id')
      ->where('m.booking_start_datetime', '<', '2022-06-07 17:00:00')
      ->where('m.booking_end_datetime', '>', '2022-06-07 15:00:00');
  })->get();

Outputs:

"select * from `vehicles` as `r` where `r`.`id` not in (select `m`.`vehicle_id` from `vehicle_rentals` as `b` inner join `vehicle_rental_times` as `m` on `b`.`vehicle_id` = `m`.`vehicle_id` where `m`.`booking_start_datetime` < ? and `m`.`booking_end_datetime` > ?)"

Please or to participate in this conversation.