Reputation: 525
Models Song and Writer
And I want to query songs such that I only get songs where ALL its writers have a certain field that is true, what would my solution be?
The Laravel whereHas function will get all songs that have at least one writer with that field, like so.
Song::whereHas('writers', function($query){
$query->where('writerField', '=', true);
});
But what is the pure eloquent way to make sure ALL writer related to a Song has that 'writerField' set to true?
Upvotes: 13
Views: 3930
Reputation: 219938
Invert your constraint, and pass the desired count to the whereHas
method:
Song::whereHas('writers', function() {
$query->where('writerField', false);
}, '=', 0);
Upvotes: 22