Skip to content
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
Merged

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

merged 1 commit into from
Jan 24, 2019

Conversation

erikgaal
Copy link
Contributor

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();

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.

None yet

2 participants