Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] "Constraining Eager Loads" callout for unsupported eager load limit() #4918

Merged
merged 1 commit into from
Jan 20, 2019
Merged

[5.7] "Constraining Eager Loads" callout for unsupported eager load limit() #4918

merged 1 commit into from
Jan 20, 2019

Conversation

derekmd
Copy link
Contributor

@derekmd derekmd commented Jan 20, 2019

The callout second sentence I came up with is kind of clunky so maybe we don't need the "why?" It's difficult to succinctly describe the groupwise-maximum query problem in a short warning alert.

This pull request covers issues:

Since 2014, GitHub issues have frequently been submitted to the framework with a misunderstanding that limit() can be used by:

  • Eloquent relationships
  • Eager load queries

The underlying issue is that developers don't realize these active record queries are not "regular" queries since they must be built and run once to fetch many Eloquent models. So the limit() call applies to fetching relations for all models and it doesn't run an SQL LIMIT per model.

(Aside from not realizing the SQL being run) I think this confusion stems from these two phrases in the docs:

  1. specify additional query constraints for the eager loading query

    "Constraints" implies that SQL keywords limit() and offset() can be used.

  2. Of course, you may call other query builder methods

    This link to https://laravel.com/docs/5.7/queries implies all methods documented on that page can be used.

We can reduce developer confusion by:

Suggesting a 3rd-party package solution

To go an extra step, the docs can link to Composer package "staudenmeir/eloquent-eager-limit" that supports eager load limit() with more recent databases. To communicate this is not a sanctioned package in the Laravel ecosystem, the docs should be clear support for that solution is external.


To call limit() while eager loading, some more recent database versions do support such groupwise-maximum database queries. Add the staudenmeir/eloquent-eager-limit dependency in your composer.json file and follow package instructions to enable this feature. Issues and support for this package should be directed to its author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants