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

[5.7] Add orWhere proxy for scopes to Eloquent Builder #27281

Merged
merged 1 commit into from Jan 24, 2019

Conversation

Projects
None yet
2 participants
@erikgaal
Copy link
Contributor

erikgaal commented Jan 23, 2019

Overview

This PR adds a shorter way of combining Model query scopes using the same technique applied in Laravel's Collections. Currently, combining model scopes with an OR operator adds more complexity in than necessary.

// scopeFoo, scopeBar, scopeBaz on the Model class 
$query->foo()->orWhere(function (Builder $query) {
    $query->bar();
})->orWhere(function (Builder $query) {
    $query->baz();
});

This PR allows chaining of OR's without the Closure in user code, like so.

$query->foo()->orWhere->bar()->orWhere->baz();

It follows the same logic as combining regular where and orWhere. The following code examples are thus considered equivalent.

// scopeFoo, scopeBar, scopeBaz on the Model class 
$query->foo()->orWhere(function (Builder $query) {
    $query->bar();
})->baz();
$query->foo()->orWhere->bar()->baz();

Considerations

  1. This PR does not break any existing functionality as far as I know.

  2. I have considered another approach to achieve the same simplicity. It would add the possibility to call query scopes prepended with or, like so. But to my knowledge, this seemed more confusing than the proposed change.

$query->foo()->orBar()->orBaz();

@taylorotwell taylorotwell merged commit 33eff6c into laravel:5.7 Jan 24, 2019

2 checks passed

continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment