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

asker's avatar
Level 2

whereDoesntHave problem

Hi, I need to exclude records (hotel rooms) from Eloquent query that have booked any day between Check In and Check Out date URL parameters. When I try this raw query in phpMyadmin everything works fine and found all rooms that have any booked day between these two dates:

SELECT * FROM jos_estateagentreservations AND '2016-05-21' < date_to AND '2016-07-17' > date_from

But, when I use this in Eloquent as a part with whereDoesntHave it shows rooms as a availiable even if they are not. Works only if Check In and Check Out dates are in the range of any of these booking records:

->whereDoesntHave('unitCalendar.unitReservation', function($query) use ($checkInDate, $checkOutDate){ $query->where('date_to','>',$checkInDate) ->where('date_from','<',$checkOutDate) ; })

Does anybody knows if whereDoesntHave not working with 2 WHERE clauses or is something else wrong? Thanks and sorry for bad english....

0 likes
6 replies
asker's avatar
Level 2

Thanks Tomo, when I put : ->whereDoesntHave('unitCalendar.unitReservation', function($query) use ($checkInDate, $checkOutDate){ $query->where('date_to','>',$checkInDate) ->where('date_from','<',$checkOutDate) ; })->get()

got an error. It is possible that is because this whereDoesntHave is the only small part of bigger eloquent query that finished with pagination: ->paginate($pagination);

PS: are you from Croatia?

tomopongrac's avatar

Error is probably because of pagination.

PS. Yes, i am from Croatia ...

asker's avatar
Level 2

OK, I willl try something else... Maybe with raw query

Thanks.

PS: da li si slobodan pomoći na projektu koji je napravljen u LARAVEL-u? Imaš tvrtku ili radiš negdje?

tomopongrac's avatar

@asker Maybe raw query is solution for your problem.

PS: Nazalost nemam nista slobodnog vremena :( ... jedva nesto ukradem vremena za laracasts

Please or to participate in this conversation.