-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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.8][WIP] Support eager loading with limit #26035
Conversation
Promising. Great job. |
This looks very nice. It'd be great if this would be in the core. |
Currently using my own version of the mysql workaround, happy to see that mysql 8.0 helps to clean the madness even if I'm on my own, but I would love that in laravel's core. For an example of use in real life, it's a needs that comes often when doing travelling best offers (like find the 3 best price for this plane/train in the next week). My current work is full of that. |
@Lelectrolux Are you using the MySQL workaround with |
@staudenmeir Currently, I'm using it on a |
I added the remaining implementations. We can use the same SQL in all four databases. The only exception is that SQL Server always requires an
|
@staudenmeir what about some |
@miki131 That's not possible for |
@staudenmeir take a look on |
Yes, but we still need a separate implementation for |
@staudenmeir You can create |
@miki131 The |
What about MariaDB? Promising feature tho |
@ludo237 MariaDB has been supporting window functions since 10.2, but they don't work in strict mode (bug report). |
Do any other frameworks support this? |
Rails doesn't support it:
I don't know about Symfony, I couldn't find anything. So probably not. |
Honestly I don't see a big incentive to take on the maintenance burden of maintaining this functionality. If it's not even supported by Rails that indicates to me it's not that widely useful. |
I released the code as a package: https://github.com/staudenmeir/eloquent-eager-limit |
Rejected as usual. Tips: To get this into the core, the best way is send a PR to Rails. |
Just hit this today, a bit confused as to why it was rejected. This would be a great - if not essential - addition to Eloquent. If people have the syntax available, it stands to reason they'll assume this is possible. |
Hey @atrauzzi, there's a package for this which you may use: https://github.com/staudenmeir/eloquent-eager-limit |
When using PostgreSQL use |
Eager loading with limit will be supported natively in Laravel 11 (#49695). |
There is currently no way to limit eager loading results per parent:
There are multiple issues about this (#16217, #4835, #18014) and it also comes up regularly on StackOverflow etc.
We can support this feature with a window function (we are already using this technique to implement
OFFSET
queries on SQL Server):The query is supported by all four databases, but requires very recent versions of MySQL and SQLite:
I wrote a sample implementation for PostgreSQL because it's the only supported database in Homestead at the moment.
The relationships override the query builder's
limit()
and call the newpartitionLimit()
method. By checking the parent model'sexists
property, we can detect eager loading. This is necessary to support lazy loading ($user->posts
). The window function would also work for lazy loading but the database might not support it.Before I implement the other databases: Would you consider this for the core?