-
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] Relax the lazy loading restrictions #37503
Conversation
So to clarify, will this mean that... $posts = Posts::limit(5)->get();
foreach ($posts as $post) {
$comments = $post->comments;
} ...the above will trigger the lazy load protection, but... $post = Posts::findOrFail(1);
$comments = $post->comments; ... will not? Because then that's really cool. |
@amcsi correct |
I may be a bit late but we've noticed something and would like to clarify if it is the intended behavior given the ff cases:
|
So, when I set preventsLazyLoading and forgot specify Also, why that count($items) is inside array_map? Does every iteration need counting? |
@Matslom I agree with you. It would be better if PreventsLazyLoading didn't check whether there was more than 1 item in the collection for determining laziness violation, but rather if it was a result that came from a query that did not use In any case the way it works now is better than nothing. We get at least some warning/reminders about performance, even if it's a bit random. |
You should use this feature only in local environments either way. This way you will be notified when there is some room for optimization. I can't think of any added value when this is being used in a production environment except it is very critical to ones infrastructure that N+1 is prevented. As per the docs:
Even with consistent behavior you wouldn't want to present your user an error page just because you forgot some lazy-loading case. |
vs documentation
may wish !== should, because it may create problems over time I'm creating app from scratch. First thing that I did was disablingLazyLoading. By reading the docs I didn't see case, why should I do this only on local env.
If it would be consistent, then I don't see case when I ship some lazy-loading cases that can break over time. |
Of course you can enable it on production. Just saying I think there are very few cases where I would actually want to, because you throw a server error where there's actually no error. It's just inefficient code in most cases. The app can still fallback to lazy loading without every breaking, like we log warnings instead of crashing the app intentionally. |
This PR will only throw the exception if the models being hydrated are more than 1. If it's only a single model then it means
N
inN+1
will always be 1. So no problem here.