-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[8.x] Add eager loading of pivot relations #37456
Conversation
I also noticed that hydrated pivot models' |
This also supports eager loading of nested pivot relations. Below is an example where it eager loads Employee::with('deductions.pivot.payrollPeriod.users.pivot.tag')->get(); |
I don't want to add more maintenance burden to Eloquent at the moment unless the feature is really compelling. In this case, I would just make your pivot model a real model. |
@taylorotwell Changing it to a real model would work, but that would be a different data structure and would require code rewrite on how it access the relation. Everytime a developer sees to add a third relation on the belongsToMany, he would be required to change the relation into hasMany Pivot model and rewrite existing code for it. While in eagerloading pivot relations, a developer can add more relations progressively without requiring a code rewrite on existing relation access which is a better developer experience. Just my 2cents tho. |
Maybe I will put this in my package for now. I have existing package which supports Laravel 6. I will create another version for the Laravel 8 support. https://github.com/ajcastro/eager-load-pivot-relations |
@taylorotwell I think this needs to be revisit since changing the pivot model to a real model is a different data structure that may not make sense. For example the important data is the belongsToMany model and not the pivot where if we change the pivot model to real model we are adding an intermediate model |
Indeed, this PR would be welcome. Transforming the pivot into a real model is still not really possible in my case for example since, as already said, it will require a big refactoring on an already very large code base. |
@taylorotwell I also think this really needs to be in the framework. Can you revisit this topic please? |
@taylorotwell This could be very helpful, I am facing this problem. Could the application be reviewed again? |
@taylorotwell We consistently have a use case for this, please revisit this topic. |
@taylorotwell bump |
@taylorotwell would like this revisited too please if possible. |
This PR provides a way for us to eager-load defined relations from a Pivot model.
Currently, we can define relations in pivot models and access them via lazy-loading, but cannot be eager-loaded.
For example in my payroll project, where an
Employee
can have manyDeduction
instances (many-to-many). But these deductions should also be set if whichPayrollPeriod
it is included.Below are sample usages:
Lazy-loading, currently supported:
But currently it cannot be eager loaded. But with this PR, we can achieve it like this:
The
BelongsToMany
relation can now detect if you intend to eager load a relation from your custom pivot model by checking if the$accessor
name exists in the$eagerLoad
variable of query builder.You can define as many relations in the Pivot model and eager-load them as many as you can as long as you make sure
to include their foreign keys in in the
withPivot()
declaration.For example let's add a
user
relation in ourEmployeeDeduction
which means the user who created this record.Then we can eager load the
payrollPeriod
and theuser
relations like so:This PR is also important in relevance with #37363 which requires us to access on eager loaded relations only.
I have an itch of solving this feature since in Laravel 5.* (laravel/ideas#175), but the setup there is different. I think this is the perfect timing for it.
I also have tested this manually on my local laravel 8 project.