Skip to content

Conversation

@rogatty
Copy link
Contributor

@rogatty rogatty commented Mar 18, 2022

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 accepts null, so tests can reset the state. Class property was already nullable so I think it's a good step anyway:

/**
* The callback that is responsible for handling lazy loading violations.
*
* @var callable|null
*/
protected static $lazyLoadingViolationCallback;

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 global Model::preventLazyLoading() does not. It seems the latter one doesn't trigger the check when using make() or create(). I'd prefer not to dive into this topic now, thus the new class in tests.

@driesvints driesvints changed the title Stop throwing LazyLoadingViolationException for recently created model instances [8.x] Stop throwing LazyLoadingViolationException for recently created model instances Mar 18, 2022
@taylorotwell taylorotwell merged commit 888947c into laravel:8.x Mar 18, 2022
@rogatty rogatty deleted the fix-handle-lazy-loading-violation branch March 18, 2022 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

handleLazyLoadingViolation shouldn't throw for newly created model instances

2 participants