From 14d6cf02bcfb9148a0a4bd52c7ba202535536c68 Mon Sep 17 00:00:00 2001 From: rogatty <7030884+rogatty@users.noreply.github.com> Date: Fri, 18 Mar 2022 08:21:49 +0100 Subject: [PATCH 1/5] Allow $lazyLoadingViolationCallback to be removed --- src/Illuminate/Database/Eloquent/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 676a08ddd1f8..d1742bbebfad 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -384,10 +384,10 @@ public static function preventLazyLoading($value = true) /** * Register a callback that is responsible for handling lazy loading violations. * - * @param callable $callback + * @param callable|null $callback * @return void */ - public static function handleLazyLoadingViolationUsing(callable $callback) + public static function handleLazyLoadingViolationUsing(?callable $callback) { static::$lazyLoadingViolationCallback = $callback; } From d05a2d0f9dab9642581248545d91865c66b4aa0f Mon Sep 17 00:00:00 2001 From: rogatty <7030884+rogatty@users.noreply.github.com> Date: Fri, 18 Mar 2022 08:22:48 +0100 Subject: [PATCH 2/5] Reset $lazyLoadingViolationCallback after test is done It was affecting other tests --- tests/Integration/Database/EloquentStrictLoadingTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Integration/Database/EloquentStrictLoadingTest.php b/tests/Integration/Database/EloquentStrictLoadingTest.php index d60976602129..6b0f0c28edb5 100644 --- a/tests/Integration/Database/EloquentStrictLoadingTest.php +++ b/tests/Integration/Database/EloquentStrictLoadingTest.php @@ -127,6 +127,8 @@ public function testStrictModeWithCustomCallbackOnLazyLoading() $models = EloquentStrictLoadingTestModel1::get(); $models[0]->modelTwos; + + Model::handleLazyLoadingViolationUsing(null); } public function testStrictModeWithOverriddenHandlerOnLazyLoading() From ed3338424d85b1d8adf5e37c3ffa673bc10eb953 Mon Sep 17 00:00:00 2001 From: rogatty <7030884+rogatty@users.noreply.github.com> Date: Fri, 18 Mar 2022 08:26:30 +0100 Subject: [PATCH 3/5] Add failing tests for lazy loading prevention --- .../Database/EloquentStrictLoadingTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Integration/Database/EloquentStrictLoadingTest.php b/tests/Integration/Database/EloquentStrictLoadingTest.php index 6b0f0c28edb5..8342c68b8438 100644 --- a/tests/Integration/Database/EloquentStrictLoadingTest.php +++ b/tests/Integration/Database/EloquentStrictLoadingTest.php @@ -143,6 +143,21 @@ public function testStrictModeWithOverriddenHandlerOnLazyLoading() $models[0]->modelTwos; } + + public function testStrictModeDoesntThrowAnExceptionOnManuallyMadeModel() + { + $model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::make(); + $model2 = EloquentStrictLoadingTestModel2::make(); + $model1->modelTwos->push($model2); + + $this->assertInstanceOf(Collection::class, $model1->modelTwos); + } + + public function testStrictModeDoesntThrowAnExceptionOnRecentlyCreatedModel() + { + $model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::create(); + $this->assertInstanceOf(Collection::class, $model1->modelTwos); + } } class EloquentStrictLoadingTestModel1 extends Model @@ -174,6 +189,19 @@ protected function handleLazyLoadingViolation($key) } } +class EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading extends Model +{ + public $table = 'test_model1'; + public $timestamps = false; + public $preventsLazyLoading = true; + protected $guarded = []; + + public function modelTwos() + { + return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id'); + } +} + class EloquentStrictLoadingTestModel2 extends Model { public $table = 'test_model2'; From dd59f62a32ec99e9451bdadc62b99834f337551a Mon Sep 17 00:00:00 2001 From: rogatty <7030884+rogatty@users.noreply.github.com> Date: Fri, 18 Mar 2022 08:27:50 +0100 Subject: [PATCH 4/5] Stop throwing when lazy loading used in recently created model --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 1cdf24375bb5..a208249f8281 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -519,6 +519,10 @@ protected function handleLazyLoadingViolation($key) return call_user_func(static::$lazyLoadingViolationCallback, $this, $key); } + if (!$this->exists || $this->wasRecentlyCreated) { + return; + } + throw new LazyLoadingViolationException($this, $key); } From 523caff684fc42b7186f7fb59fdcb964f54b2a30 Mon Sep 17 00:00:00 2001 From: rogatty <7030884+rogatty@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:06:27 +0100 Subject: [PATCH 5/5] Fix formatting --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index a208249f8281..c9d12ae8f474 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -519,7 +519,7 @@ protected function handleLazyLoadingViolation($key) return call_user_func(static::$lazyLoadingViolationCallback, $this, $key); } - if (!$this->exists || $this->wasRecentlyCreated) { + if (! $this->exists || $this->wasRecentlyCreated) { return; }