-
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
Eloquent: Nested Lazy Eager Loading will erase relations from Nested Eager Loading #24121
Comments
That's the intended behavior. Use this:
|
Thank you for your quick reply. Unfortunately its plain wrong. It still is a bugWhy? Nested Eager Loading is well documented. Lazy Loading, as well. I suggest one of those ways forwardEither fix the bug or document it here: https://laravel.com/docs/5.6/eloquent-relationships#lazy-eager-loading some more explanation:
Resulting bug: You will not find any 'adjunctProducts'-Relations in I would really appreciate if someone could take a serious look at this. I am happy to help and provide more information as needed. |
Sorry, I missed that. Use this:
The purpose of I can see why you would expect a different behavior. For a single model and non-nested relationships you can use the Your situation would require You're welcome to propose or implement improvements. |
Thank you very much for elaborating on that! |
Again, my bad:
A shorter way to achieve this would definitely be nice... |
Perfect! |
I updated my post. One would think that three attempts should be enough to write a working one-liner... |
One has to admit, its certainly not the most obvious one-liner... Thanks! 🥇 |
I proposed |
Description:
When first using Nested Eager Loading, like
(1)
$categories = Category::with(['products.adjunctProducts'])->get();
and put a Nested Lazy Eager Loading on top of it, like
(2)
$categories->load(['products.subscriptionModel']);
then
after (1) you will get a Collection with 'products.adjunctProducts' relation, as expected.
But after (2) you will get a Collection with 'products.subscriptionModel' relation (again as expected),
but you will not find the relation 'products.adjunctProducts' anymore.
Remarks:
$categories = Category::with('products')->get();
with()
'.The described case here is simplified as much as possible. Our Eager Loaded collection normally can be changed by different modules using an event. So if a modules needs further relations it would just use
load()
get()
(until after the event) but work with the Builder-Object and concatenate severalwith()
s. Not sure if that's the best solution!?Steps To Reproduce:
Take a model with different relations on a first relation and reproduce with the description above.
The text was updated successfully, but these errors were encountered: