[12.x] Add whereHasAny
, orWhereHasAny
, whereHasAll
, and orWhereHasAll
Methods to Eloquent Builder
#55563
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Even though PR #55303 was rejected, I truly believe that Eloquent should offer native support for whereAny across relationships.
Working with complex nested relationships is a daily reality for many developers building real-world applications.
Currently, chaining multiple whereHas and orWhereHas conditions manually makes the code repetitive, harder to read, and more prone to mistakes.
Adding first-class support for whereHasAny, orWhereHasAny, whereHasAll, and orWhereHasAll would not only simplify our queries — it would empower developers to express their intentions more clearly, with better performance and less boilerplate.
I genuinely believe that Eloquent deserves this improvement.
It would make a real difference for many of us working with complex data structures every day.
This PR introduces four new methods to the Eloquent Builder:
These new methods allow for more expressive and concise querying when filtering models based on multiple nested relationships combined by "any" (OR) or "all" (AND) logic across specific columns.
Motivation
Previously, to perform a search across multiple nested relationships, developers had to manually chain multiple whereHas and orWhereHas statements, leading to repetitive, verbose, and harder-to-maintain code.
This feature improves readability, developer experience, and maintainability, while keeping the flexibility and performance of Eloquent queries.
Before:
After (with new whereHasAny)
Comparison across different relationships
One powerful advantage of whereHasAll is that it allows you to compare the same value across different relationships.
This is extremely useful for real-world scenarios, such as when the same identifier (code, reference, etc.) needs to match across multiple related tables.
For example, suppose a User has both shipments and orders, and you want to find users where both a shipment and an order have the same code:
Before
After (with new whereHasAll)
Notes about columns
Internally, the
whereHasAny
,orWhereHasAny
,whereHasAll
, andorWhereHasAll
methods instantiate their conditions using the existingwhereAny
andwhereAll
methods from the Builder class.This approach offers maximum flexibility when filtering nested relationships, supporting direct column names, explicit table.column syntax for disambiguation, and full custom query logic via closures.
Please feel free to share your thoughts — I would love to hear your feedback!