[8.x] Stop throwing LazyLoadingViolationException for recently created model instances #41549
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #41420. I think the issue describes the problem and the solution pretty well.
In addition, I had to make a change in
Model::handleLazyLoadingViolationUsing
which now acceptsnull
, so tests can reset the state. Class property was already nullable so I think it's a good step anyway:framework/src/Illuminate/Database/Eloquent/Model.php
Lines 170 to 175 in dd59f62
At first, I was surprised that new tests were passing without any changes in the code. Then I figured out that only a local
public $preventsLazyLoading = true;
causes the issue, while globalModel::preventLazyLoading()
does not. It seems the latter one doesn't trigger the check when usingmake()
orcreate()
. I'd prefer not to dive into this topic now, thus the new class in tests.