[6.x] Improve augmentation of relationships #435
Merged
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.
This pull request refactors how Eloquent Relationships are augmented to improve performance and prevent as many duplicate queries, like seen in #430.
Changes include:
HasMany
/BelongsTo
relationship instance being passed to the augmentation methods, we now pass anEloquent\Collection
instance.$model->relationship
instead of$model->relationship()
. This change also lets us take advantage of eager loading for the related models.BaseFieldtype
to simplify it, now it only needs to accept anEloquent\Collection
or an array of IDs (in the case the fieldtype is being used on entries).runwayUri
relationship no longer gets augmented.runwayUri
relation to be augmented in the first place so it's a quick win 🚀{{ runway }}
tag wasn't eager loading any relationships, unless you were providing the relationships to be eager loaded yourself.Testing
On my local sandbox, I managed to reduce the number of total queries for index pages (which use the
{{ runway }}
tag) down from a couple hundred to 3 queries.Then, in the demo site provided in #430, I've managed to get rid of the duplicate queries & reduce the total number of queries down to 108, which is more reasonable than a couple thousand.
Closes #430.