[13.x] feat: add chaperone support for BelongsToMany pivot models - #61152
Open
calebdw wants to merge 1 commit into
Open
[13.x] feat: add chaperone support for BelongsToMany pivot models#61152calebdw wants to merge 1 commit into
calebdw wants to merge 1 commit into
Conversation
Add the ability to automatically set the declaring and related model instances as loaded relationships on custom pivot models, mirroring the chaperone concept from HasMany but adapted for the dual-parent nature of many-to-many relationships. This eliminates extra queries when a custom pivot class defines belongsTo relationships back to either side of the many-to-many. Both relation names can be explicitly provided or auto-guessed from the foreign key column names and model class basenames. If guessing fails for a side, that side is silently skipped; if an explicit name is invalid, a RelationNotFoundException is thrown. The implementation lives in a new SupportsPivotInverseRelations trait used by BelongsToMany, with hooks in hydratePivotRelation() for lazy loading and match() to correct the declaring side during eager loading where $this->parent only reflects the first parent model.
calebdw
force-pushed
the
calebdw/push-tqvuqtxvszyk
branch
from
August 15, 2026 23:58
1db8eed to
423b455
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.
Hello!
Many times when using custom pivot models, the BelongsTo relations can be defined on the model---however, trying to use them requires loading the models from the database even though both of the models are available during the BelongsToMany hydration
This PR adds
chaperonesupport to theBelongsToManyrelation, if a custom pivot model is not provided then calling->chaperone()is a no-op. Any relations that don't exist are skipped (some pivot models only define one relation or even no relations); however, if a relation name is explicitly provided and the corresponding relation does not exist then an exception is thrownThanks!