Skip to content

Commit f26816d

Browse files
authored
relax the lazy loading restrictions (#37503)
1 parent e0402fc commit f26816d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,12 @@ public function hydrate(array $items)
351351
{
352352
$instance = $this->newModelInstance();
353353

354-
return $instance->newCollection(array_map(function ($item) use ($instance) {
354+
return $instance->newCollection(array_map(function ($item) use ($items, $instance) {
355355
$model = $instance->newFromBuilder($item);
356356

357-
$model->preventsLazyLoading = Model::preventsLazyLoading();
357+
if (count($items) > 1) {
358+
$model->preventsLazyLoading = Model::preventsLazyLoading();
359+
}
358360

359361
return $model;
360362
}, $items));

tests/Integration/Database/EloquentStrictLoadingTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public function testStrictModeThrowsAnExceptionOnLazyLoading()
4848
$models[0]->modelTwos;
4949
}
5050

51+
public function testStrictModeDoesntThrowAnExceptionOnLazyLoadingWithSingleModel()
52+
{
53+
EloquentStrictLoadingTestModel1::create();
54+
55+
$models = EloquentStrictLoadingTestModel1::get();
56+
57+
$this->assertInstanceOf(Collection::class, $models);
58+
}
59+
5160
public function testStrictModeDoesntThrowAnExceptionOnAttributes()
5261
{
5362
EloquentStrictLoadingTestModel1::create();
@@ -85,6 +94,8 @@ public function testStrictModeDoesntThrowAnExceptionOnSingleModelLoading()
8594
{
8695
$model = EloquentStrictLoadingTestModel1::create();
8796

97+
$model = EloquentStrictLoadingTestModel1::find($model->id);
98+
8899
$this->assertInstanceOf(Collection::class, $model->modelTwos);
89100
}
90101

@@ -95,6 +106,7 @@ public function testStrictModeThrowsAnExceptionOnLazyLoadingInRelations()
95106

96107
$model1 = EloquentStrictLoadingTestModel1::create();
97108
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
109+
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
98110

99111
$models = EloquentStrictLoadingTestModel1::with('modelTwos')->get();
100112

0 commit comments

Comments
 (0)