-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
whereHas with nested relation is broken #24407
Comments
ps: I think why no tests have caught this is because the tests only test with a self-referencing relation! See:
|
It works for me on 5.5 and 5.6. Can you post the models? |
Set up a clean laravel project and got a reproducable case, will put code on github in a moment. |
Here it is: The Spatie Medialibrary lets you tie files to anything. If you however want to use one file on multiple items in your application, you have to use some middle model, the FileAssociator in my reproducing repo. Now, if you want to select files which only have a page with status '1', you use the code from That fails, with: Call to undefined method Illuminate\Database\Query\Builder::pages() |
While certainly interesting, it seems some parts DO work, and this counts as a separate, possibly fixable, issue.
Produces (I think the expected) query:
|
To be more specific: As described in the linked issues, you would have to query multiple tables. |
@tillkruss , can you clarify? This is not a duplicate. @staudenmeir was talking about MorphTo, but we're going the other way if I recall correctly. It almost works! |
You wrote that |
You're right, sorry, I got "obsessed" by the the fact the builder was doing multiple things on the File model, and I saw the FileAssociator example working. I'll close this as a duplicate of the issues you mentioned. |
Description:
When working with relations, there is the
whereHas
method, constraining things on a relation, described here:https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence
There should be support for nested relations, but this will fail with an 'undefined function on Builder'. I looked into this.
Steps To Reproduce:
Model A belongsTo B
Model B belongsToMany C
When doing
A::with('b.c')->whereHas('b.c', function($q){})->get();
we see failure of the 'undefined function c'.
I looked into this and saw in the
QueriesRelationship
trait multiple calls togetRelationWithoutConstraints
. There we have$this->getModel()
and in the reproduction case we see that returning A and A, not A and B. I think that's part of the problem.Sorry I can't provide a reproducing repo here on Github, no time today.
The text was updated successfully, but these errors were encountered: