A
A
AkifPetrov2018-12-20 16:00:44
Laravel
AkifPetrov, 2018-12-20 16:00:44

Is it possible to add a condition on the number of child records in whereDoesntHave?

Hello,
Can you please tell me if it is possible to select records in which the number of child records is less than or equal to 1 using whereDoesntHave?

$room = Room::whereDoesntHave('bookings', function($query) use ($request) {
        $query->where('end', '>=', $request->start_date);
        $query->where('start', '<=', $request->end_date);
    }
);

I need for the booking system to select all rooms in which no more than one person lives in a given period of time.
That is, we need whereDoesntHave with count < 2
Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Mashletov, 2018-12-20
@AkifPetrov

Room::whereHas('bookings', function($query) use ($request) {
    $query->where('end', '>=', $request->start_date);
    $query->where('start', '<=', $request->end_date);
}, '<=', 1)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question