Skip to content

Commit 888947c

Browse files
authored
[8.x] Stop throwing LazyLoadingViolationException for recently created 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
1 parent 126518b commit 888947c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ protected function handleLazyLoadingViolation($key)
519519
return call_user_func(static::$lazyLoadingViolationCallback, $this, $key);
520520
}
521521

522+
if (! $this->exists || $this->wasRecentlyCreated) {
523+
return;
524+
}
525+
522526
throw new LazyLoadingViolationException($this, $key);
523527
}
524528

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ public static function preventLazyLoading($value = true)
384384
/**
385385
* Register a callback that is responsible for handling lazy loading violations.
386386
*
387-
* @param callable $callback
387+
* @param callable|null $callback
388388
* @return void
389389
*/
390-
public static function handleLazyLoadingViolationUsing(callable $callback)
390+
public static function handleLazyLoadingViolationUsing(?callable $callback)
391391
{
392392
static::$lazyLoadingViolationCallback = $callback;
393393
}

tests/Integration/Database/EloquentStrictLoadingTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public function testStrictModeWithCustomCallbackOnLazyLoading()
127127
$models = EloquentStrictLoadingTestModel1::get();
128128

129129
$models[0]->modelTwos;
130+
131+
Model::handleLazyLoadingViolationUsing(null);
130132
}
131133

132134
public function testStrictModeWithOverriddenHandlerOnLazyLoading()
@@ -141,6 +143,21 @@ public function testStrictModeWithOverriddenHandlerOnLazyLoading()
141143

142144
$models[0]->modelTwos;
143145
}
146+
147+
public function testStrictModeDoesntThrowAnExceptionOnManuallyMadeModel()
148+
{
149+
$model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::make();
150+
$model2 = EloquentStrictLoadingTestModel2::make();
151+
$model1->modelTwos->push($model2);
152+
153+
$this->assertInstanceOf(Collection::class, $model1->modelTwos);
154+
}
155+
156+
public function testStrictModeDoesntThrowAnExceptionOnRecentlyCreatedModel()
157+
{
158+
$model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::create();
159+
$this->assertInstanceOf(Collection::class, $model1->modelTwos);
160+
}
144161
}
145162

146163
class EloquentStrictLoadingTestModel1 extends Model
@@ -172,6 +189,19 @@ protected function handleLazyLoadingViolation($key)
172189
}
173190
}
174191

192+
class EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading extends Model
193+
{
194+
public $table = 'test_model1';
195+
public $timestamps = false;
196+
public $preventsLazyLoading = true;
197+
protected $guarded = [];
198+
199+
public function modelTwos()
200+
{
201+
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
202+
}
203+
}
204+
175205
class EloquentStrictLoadingTestModel2 extends Model
176206
{
177207
public $table = 'test_model2';

0 commit comments

Comments
 (0)