$cars = Car::query()
->whereDoesntHave('booking', function (Builder $builder) use ($bookingCriteria) {
$builder->where('pick_up_date', '<=', $bookingCriteria['toDate'])
->where('drop_off_date', '>=', $bookingCriteria['fromDate']);
})
->get();
Nov 12, 2022
3
Level 14
Help with whereDoesntHave query
Hi, I'm trying to create a table which holds records of assigned cars. So when a car is assigned to a booking it gets added to this table assigned_cars which is a Car belongsToMany Bookings relationship.
But what I'm struggling with is the query to get available cars. Doesn't matter what the pick-up date is. It seems like if the car has already been booked once before that it will not show again in the list. This is how my query looks like:
$cars = Car::query()
->whereDoesntHave('booking', function (Builder $builder) use ($bookingCriteria) {
$builder->where('pick_up_date', '<=', $bookingCriteria['toDate']);
$builder->where('drop_off_date', '>=', $bookingCriteria['fromDate']);
})
->get();
Any idea how to fix this query?
Level 22
Please or to participate in this conversation.