-
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
[12.x] Added Automatic Relation Loading (Eager Loading) Feature #53655
base: master
Are you sure you want to change the base?
Conversation
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a very desired and powerful feature. It's going to save a lot of effort. I hope it will be approved soon. Thank you so much!
I'm not sure I understand this -- isn't this typical Eloquent lazy-loading behaviour? Won't the relations in your example be loaded via lazy-loading? Ex, this should work already: $orders = Order::all();
foreach ($orders as $order) {
echo $order->client->owner->company->name;
} Or are you referring to preventing the possibility from N+1'ing due to the above lazy loading? Please correct me if I'm misunderstanding 🙏 |
You’re absolutely right, and typically, methods like In such cases, it can become difficult to track and manually specify which relations should be eager-loaded, especially if those relations are deeply nested or dynamically used. The In your example: $orders = Order::all();
foreach ($orders as $order) {
echo $order->client->owner->company->name;
} If there are 10 orders, here’s what happens: With It's very simple example, we can just use with the same result $orders->load('client.owner.company'); But if we change our code in the future, the relations will be loaded unnecessarily until we explicitly remove them from the load. foreach ($orders as $order) {
echo $order->client->name;
} Let me know if this makes sense or if you’d like further clarification! 🙏 |
I am not sure if I fully understand this, so sorry in advance if I don't make sense. But if this works as you say, wouldn't it make sense to replace the current lazy loading behaviour instead of making your feature opt-in? |
Thanks for the question! The feature is opt-in to avoid forcing developers who prefer manual control over relation loading and to preserve the existing lazy-loading behavior. This also helps prevent potential issues in existing projects relying on the default behavior. However, if desired, you can enable it globally for the entire project using |
This is a great idea especially for junior devs to avoid N+1 issues |
Isn't that essentially what the Eager Loading by Default (from the Laravel docs)
|
Description
In large projects it can become difficult to track and manually specify which relations should be eager-loaded, especially if those relations are deeply nested or dynamically used. Therefore, automatic relation loading can be useful.
Challenges include:
New
withRelationAutoload()
MethodA new method, withRelationAutoload(), has been added to models and Eloquent collections. When called, it automatically loads relations whenever they are accessed, without the need for explicit load() or with() calls.
Example:
Support for Morph Relations
The feature works seamlessly with polymorphic relations. It only loads the specific morph type that is accessed, ensuring efficient use of resources.
Doesn’t Break Manual Relation Loading
Users can still manually load relations using load() or with() before accessing the relation. If a relation is already loaded manually, it won’t be reloaded.
Global Automatic Loading
For cases where you want automatic loading enabled across all models, you can use the static method `
This feature significantly simplifies working with relations and reduces the overhead of managing eager loading in Laravel projects, enabling developers to focus on application logic instead of data loading mechanics.
If you have suggestions for better method names, feel free to share them!