PHPLARA-257 Replay query macros when eager loading MorphTo relations - #3559
Open
GromNaN wants to merge 1 commit into
Open
PHPLARA-257 Replay query macros when eager loading MorphTo relations#3559GromNaN wants to merge 1 commit into
GromNaN wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes eager loading of MorphTo relations when query macros (notably withTrashed()) must be replayed per morph type, aligning MongoDB behavior with upstream Laravel and restoring buffered macro/constraint behavior during eager loading.
Changes:
- Removes the MongoDB-specific
MorphTo::getResultsByType()override so the package inherits Laravel’s macro-replay implementation. - Adds a regression test covering lazy vs eager loading for
morphTo()->withTrashed()against a soft-deleted related model. - Extends the test
Photomodel with ahasImageWithTrashed()relation for the new test.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/RelationsTest.php | Adds a regression test for eager-loaded morphTo()->withTrashed() returning soft-deleted related models. |
| tests/Models/Photo.php | Adds a hasImageWithTrashed() morph-to relation used by the new test. |
| src/Relations/MorphTo.php | Removes the custom getResultsByType() override to rely on Laravel’s implementation (enabling macro replay/constraint merging). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
getResultsByType() was a stripped-down copy of the Laravel implementation, unchanged since 2015. Building the query with a plain newQuery() dropped replayMacros(), which is the only way withTrashed(), withoutTrashed(), onlyTrashed() and buffered select() calls reach the related model query. It also dropped mergeConstraintsFrom(), with() and withCount(), silently turning morphWith(), morphWithCount() and constrain() into no-ops. The two reasons for the override no longer apply: DocumentModel ::qualifyColumn() returns the column unchanged, so the parent's $instance->qualifyColumn($ownerKey) is already correct, and whereInMethod() is separately overridden to return whereIn. Fixes mongodb#3454 Fixes mongodb#1361 Co-authored-by: Steve Porter <steve@steveporter.io>
GromNaN
force-pushed
the
phplara-257-morphto-eager-macros
branch
from
July 29, 2026 12:23
fee8ae0 to
0513917
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PHPLARA-257
Eager loading a
morphTo()relationship withwithTrashed()returnednullinstead of the soft-deleted related model. Lazy loading was not affected.MongoDB\Laravel\Relations\MorphTo::getResultsByType()was a stripped-down copy of the Laravel implementation, unchanged since 2015. It built the query with a plain$instance->newQuery(), which dropsreplayMacros(). That call is what makeswithTrashed()work:MorphTocannot apply it to the parent query because the macro only exists on the related model, so it buffers the call and replays it once per morph type. Without the replay theSoftDeletingScopeis never removed.The same omission silently disabled
mergeConstraintsFrom(),with()andwithCount(), which mademorphWith(),morphWithCount()andconstrain()no-ops on MongoDB.The two reasons the override existed no longer apply:
DocumentModel::qualifyColumn()returns the column unchanged, so the parent's$instance->qualifyColumn($ownerKey)is already correct for MongoDB.whereInMethod()is separately overridden to returnwhereIninstead ofwhereIntegerInRaw.So the override is removed and the Laravel implementation is inherited. Tests cover
withTrashed(),constrain()andmorphWith(), all three failing on the previous code.Fixes #3454
Fixes #1361