Skip to content

Conversation

@Jade-GG
Copy link
Collaborator

@Jade-GG Jade-GG commented Jan 5, 2026

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:

  • We don't eager load the entry data for everything, so when the retrieved event is fired for each model individually we also end up loading the data for each model individually.
  • The retrieved event is fired before any eager loading has been done.1

To 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 throwMissingAttributeExceptionIfApplicable function2 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

  1. See also: github.com/Eloquent emits event retrieved prior to finishing the entire retrieval process of eager loaded relations laravel/framework#29658

  2. Same function we use for custom attributes in Rapidez v5: https://github.com/rapidez/core/blob/master/src/Models/Traits/HasCustomAttributes.php#L202-L205

BobWez98
BobWez98 previously approved these changes Jan 5, 2026
@royduin royduin requested a review from indykoning January 6, 2026 12:02
@royduin
Copy link
Member

royduin commented Jan 13, 2026

Please review @indykoning and @kevinmeijer97

kevinmeijer97
kevinmeijer97 previously approved these changes Jan 13, 2026
protected function entry(): Attribute
public function entry(): BelongsTo
{
if (!app()->runningInConsole()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this can go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Jade-GG Jade-GG dismissed stale reviews from kevinmeijer97 and BobWez98 via 7739c7f January 19, 2026 10:11
BobWez98
BobWez98 previously approved these changes Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants