Level 1
You can use whereHas:
$roomsWithTrueStatus = App\Booking::whereHas('rooms', function ($query) {
$query->where('status', '=', true);
})->count();
Summer Sale! All accounts are 50% off this week.
I have a Booking model which has hasMany relation called rooms, I want to check if all the rooms of that booking have status true, How can I do that in eloquent? I have come up with counting all the rooms in the booking model and getting the count of all the rooms with the status true and comparing the count. Is there any better solution?
You can use every method on your rooms relation
every()
The every method may be used to verify that all elements of a collection pass a given truth test:
collect([1, 2, 3, 4])->every(function ($value, $key) {
return $value > 2;
});
// false
Please or to participate in this conversation.