Skip to content

Commit

Permalink
[8.x] Stop throwing LazyLoadingViolationException for recently create…
Browse files Browse the repository at this point in the history
…d model instances (#41549)

* Allow $lazyLoadingViolationCallback to be removed

* Reset $lazyLoadingViolationCallback after test is done

It was affecting other tests

* Add failing tests for lazy loading prevention

* Stop throwing when lazy loading used in recently created model

* Fix formatting
  • Loading branch information
rogatty committed Mar 18, 2022
1 parent 126518b commit 888947c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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

0 comments on commit 888947c

Please sign in to comment.