-
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
Wrong class instance when eager loading a nullable polymorphic relation with withDefault() #27369
Comments
It really doesn't makes sense to make any of those two columns nullable imo. If you have a specific example which you feel should be supported I suggest you open up an issue at laravel/ideas as this is more of a feature request then a bug. I also believe we should look at maybe supporting the |
@driesvints I agree that this is a rare edge case, but I still think it is a bug:
I'll try to summarize the use case I have for this without going into details of a complicated system that caters to a niche userbase. The Technically, I could create blank instances of |
I still think that this is just a matter of doing things differently and just associate the model with the polymorph at a later time. |
Well, I have been able to rely on the lazy loading behavior for months of development before encountering the issue with eager loading, so there's too much work invested to change the approach at this point. Currently, I use the solution from the PR by extending the MorphTo class in my project. Also, I don't understand how this is less of bug than #24725, which has the "bug" label despite being more of an idea. In fact, the approach used here - specifying a morph type without an id - is a straightforward (and already implemented for the most part) solution to the problem of how |
Description:
I have a use case for the MorphTo relation which sometimes involves setting an explicit morph type, but leaving the morph id blank. This way I can expect that accessing the relation will always provide an instance of the appropriate class. It works on individual instances, but fails with eager loading: instead of an instance of the specified morph type, you get an instance of the parent model.
Steps To Reproduce:
Minimal setup:
Reproduction in
tinker
:Expected behavior:
Ideally, the
ParentModel
instance fromApp\ParentModel::query()->with('morph')->first()
should have themorph
relation set to an instance ofApp\TypeOne
.Alternatively, the eager loading process could ignore the
morph
relation when no id is present so that the correct instance could be lazy loaded on first access.P.S.: The wrong behavior is also described in #24725, but whereas that involves an open-ended question about how to handle the edge case where morph type is also undefined, this issue is more clearly a bug: there are expected behaviors that the system is not following.
The text was updated successfully, but these errors were encountered: