Skip to content

[5.2] Wrap "has" callback in where closure #13785

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
wants to merge 3 commits into from
Closed

Conversation

wleona3
Copy link
Contributor

@wleona3 wleona3 commented May 30, 2016

So that "orWheres" on the has condition don't have to ALSO be nested in another where closure.

wleona3 added 3 commits May 30, 2016 17:16
…e has condition don't have to ALSO be nested in another where closure.
@acasar
Copy link
Contributor

acasar commented May 30, 2016

Hmm... Can you give some code example where this is needed?

You can have joins and other clauses in whereHas so we can't nest everything inside where

@GrahamCampbell GrahamCampbell changed the title Wrap "has" callback in where closure [5.2] Wrap "has" callback in where closure May 30, 2016
@taylorotwell
Copy link
Member

Closing based on above comment.

@wleona3
Copy link
Contributor Author

wleona3 commented May 30, 2016

Code such as this:

Model::whereHas('relation', function ($query) {
    $query->where('foo', 'bar');
    $query->orWhere('foo', 'baz');
});

Results in an exists condition like this:
exists (relation.id = model.relation_id and relation.foo = 'bar' or relation.foo = 'baz')

The issue here is that the 'or' statement now always applies. I'd hope that it'd generate something similar to

exists (relation.id = model.relation_id and (relation.foo = 'bar' or relation.foo = 'baz'))

However I can see how to PR doesn't solve this

@acasar
Copy link
Contributor

acasar commented May 30, 2016

@wleona3 Thanks for the example. Your fix doesn't work in some cases as mentioned above, but we could implement it more like this: https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Builder.php#L1149

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.

3 participants