Fix N+1 query by using relation data#179
Merged
Merged
Conversation
Jade-GG
commented
Jan 5, 2026
BobWez98
previously approved these changes
Jan 5, 2026
Member
|
Please review @indykoning and @kevinmeijer97 |
indykoning
reviewed
Jan 13, 2026
kevinmeijer97
previously approved these changes
Jan 13, 2026
| protected function entry(): Attribute | ||
| public function entry(): BelongsTo | ||
| { | ||
| if (!app()->runningInConsole()) { |
Collaborator
Author
There was a problem hiding this comment.
I assume this was for making sure no database queries get done when doing commands in pipelines right? I don't see where this would be a problem in this case with it being a relation now.
BobWez98
previously approved these changes
Jan 21, 2026
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.
ref: GT-2423
We ran into an issue with a massive N+1 query on the overview page of a runway resource. This was even more apparent when showing 500 results per page.
The reason for this is twofold:
retrievedevent is fired for each model individually we also end up loading the data for each model individually.retrievedevent is fired before any eager loading has been done.1To fix this, I've turned the entry attribute into a relation that gets eager loaded, and I've also deferred the actual retrieving of the data from this relation to the
throwMissingAttributeExceptionIfApplicablefunction2 so that we only start querying this data when the relation is loaded.Note that the relation is a bit hacky because we need to get the related key from the data json. Laravel doesn't allow for raw statements to be used as foreign keys, so this uses a table joined onto itself to add the variable from the json column as a regular column, which we can then use as regular foreign key.
Footnotes
See also: github.com/Eloquent emits event retrieved prior to finishing the entire retrieval process of eager loaded relations laravel/framework#29658 ↩
Same function we use for custom attributes in Rapidez v5: https://github.com/rapidez/core/blob/master/src/Models/Traits/HasCustomAttributes.php#L202-L205 ↩