diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 1cdf24375bb5..c9d12ae8f474 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); } 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; } diff --git a/tests/Integration/Database/EloquentStrictLoadingTest.php b/tests/Integration/Database/EloquentStrictLoadingTest.php index d60976602129..8342c68b8438 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() @@ -141,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 @@ -172,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';