Skip to content

[12.x] Add whereRelationAny, whereRelationAll to Eloquent Builder #55303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

ezequidias
Copy link

@ezequidias ezequidias commented Apr 6, 2025

This PR introduces four new methods to the Eloquent Builder:

  • whereRelationAny
  • orWhereRelationAny
  • whereRelationAll
  • orWhereRelationAll

These methods are the equivalents of the existing whereAny and whereAll methods available on the base Query Builder.

They allow for more expressive and cleaner filtering when querying related models with multiple conditions, especially when using nested or grouped clauses on relationships.

Before:

Post::query()
  ->whereHas('user', fn($query) => 
    $query->whereAny(['first_name', 'last_name', 'username'], $search)
  );
  
// or
 
Post::query()
  ->whereRelation('user', 'first_name', $search)
  ->orWhereRelation('user', 'last_name', $search)
  ->orWhereRelation('user', 'username', $search);

After:

Post::query()
  ->whereRelationAny('user', ['first_name', 'last_name', 'username'], $search);

These methods significantly improve readability and developer productivity by avoiding complex closures when applying multiple relationship filters.

Key Benefits:

  1. Simplifies Relationship Queries: Reduces nested closures and repetitive whereHas/orWhereHas calls.
  2. Consistent Syntax: Matches the existing whereAny/whereAll conventions in the Query Builder.
  3. Improved Readability: Clear intent when querying relationships with ANY/ALL logic.

These methods maintain backward compatibility and follow Laravel’s coding standards.

@ezequidias ezequidias marked this pull request as draft April 6, 2025 23:38
@ezequidias ezequidias changed the title Add whereRelationAny, whereRelationAll to Eloquent Builder [12.x] Add whereRelationAny, whereRelationAll to Eloquent Builder Apr 7, 2025
@ezequidias ezequidias marked this pull request as ready for review April 7, 2025 02:01
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants