[9.x] Add pending has-many-through and has-one-through builder #45894
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.
Alternative to and inspired by #45826
This PR takes a slightly different approach. Everything is just using standard
HasMany
orHasOne
relationships with standard key ordering. Essentially there is no new concept to learn.I also feel by starting the chain with "through" it personally helps my mental model of what is happening.
"From A through B give me C"
HasManyThrough
Using the examples from the docs, and assuming we have the relationships already defined...
This allows us to use those relationships to define our has-many-through relationship on the
Project
:The closure passed to the
has()
method receives an instance of theEnvironment
model.HasOneThrough
There is not a new API for this method. Using the example from the docs...
We may use these existing relationships to create a hasOneThrough relationship.
Creating relationships on the fly
These examples have all shown usage of exiting model relationships, but it is also possible to use this when relationships that you create on the fly. The additional keys are not required here, they are just there for illustration...
This means that the ordering or has many through disappears and all a developer needs to know is the standard parameters of
HasMany
andHasOne
.String / Higher order API
The following APIs are all equivalent.