-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Eager Loading Polymorphic Relation's Related Models #6368
Comments
Does anyone have anything to say here? |
The question and accepted answer on SO outline the issue clearly—it would be useful to be able to eager load distant relations of a polymorphic model, either using the dot syntax (preferably) or some by some other means. It is possible to do now, but only with a hack involving extra models and accessors. Or, at the very least, if adding this functionality isn't practical, then trying to chain relations when using a polymorphic model should throw an exception. At the moment, the first relation is eager loaded as expected, but the model after the dot is simply ignored. |
👍 Eager load should care about relationship type - is it 1 to 1, N to N, or polymorphic, and would load it correctly. Calling |
Hi, I just bumped into this problem and my first "need" would be at least a raised Exception. Any news on that proposal ? |
+1 for being able to eager load a polymorphic model's nested relations using dot notation |
Closing due to 6 months of inactivity and the PR being rejected. |
Hey I've just come across this issue and am having similar problems. What i need to be able to do is load nested relations on a morphTo() relation. For example, i have the following model:
The model() relation may return many different types of model, one of which is a User. I need to be able to load the "profile" of a user in the normal ->with() way. For example:
Currently this would just ignore the profile relation. The workaround i have found is by customising the "getResultsByType" method of the MorphTo relation object as follows:
References to this model can then be overridden in the base Model class so that the custom relation is used. This enables me to load nested relations on morphTo() models. Maybe others will be able to use this to solve their own issues. If you agree with the way this works and are interested in implementing this functionality, i can make it into a pull request @GrahamCampbell / @taylorotwell |
@20TRIES I just tried this with my project as well and it works as expected with nested polymorphic related models. Don't know if it messes up things in other places though. |
@20TRIES @OskarPersson I fixed this in #13737, tagged in 5.2.34 |
Hi @taylorotwell,
I can eager load polymorphic relations/models without any n+1 issues. However, if I try to access a model related to the polymorphic model, the n+1 problem appears and I can't seem to find a fix.
Here is an easily reproducible example I put together: http://stackoverflow.com/questions/26727088/laravel-eager-loading-polymorphic-relations-related-models/26825205#26825205
I am sure the laravel community would love to be able to eager load those polymorphic models related models using the same familiar syntax as this: $histories = History::with('historable.company')->get(); where company is a polymorphic models related model.
For now, I went with the accepted stack overflow answer since calling it like that was not possible.
Please let us know your thoughts on this.
The text was updated successfully, but these errors were encountered: