"Where Has All" functionality in Laravel

Published under Laravel.

By default, a whereHas query checks to see that the given relation has at least one row for the given constraint, ie:

The above query will return posts that were authored by authors with ID 1 or 2. If, however, we want to find the posts that were authored by both authors 1 and 2, we need to change the query to tell Laravel to include posts that return 2 author relation rows when filtered:

The above query tells Laravel to load the posts that have 2 author relation records when the author relation records are filtered by the given array of IDs.

Thanks for reading!

Did you find this post useful? Let me know on Twitter! If you found an issue with the content, submit a pull request!

Subscribe to my newsletter to know when I publish more content in the future.

❤️ Likes: 0
📣 Retweets: 0
💬 Replies: 0
🙊 Mentions: 0