Skip to content
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] Stop throwing LazyLoadingViolationException for recently created model instances #41549

Merged
merged 5 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Expand Up @@ -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;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/Database/EloquentStrictLoadingTest.php
Expand Up @@ -127,6 +127,8 @@ public function testStrictModeWithCustomCallbackOnLazyLoading()
$models = EloquentStrictLoadingTestModel1::get();

$models[0]->modelTwos;

Model::handleLazyLoadingViolationUsing(null);
}

public function testStrictModeWithOverriddenHandlerOnLazyLoading()
Expand All @@ -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
Expand Down Expand Up @@ -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';
Expand Down