Skip to content

Commit

Permalink
relax the lazy loading restrictions (#37503)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored May 27, 2021
1 parent e0402fc commit f26816d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,12 @@ public function hydrate(array $items)
{
$instance = $this->newModelInstance();

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

$model->preventsLazyLoading = Model::preventsLazyLoading();
if (count($items) > 1) {
$model->preventsLazyLoading = Model::preventsLazyLoading();
}

return $model;
}, $items));
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Database/EloquentStrictLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public function testStrictModeThrowsAnExceptionOnLazyLoading()
$models[0]->modelTwos;
}

public function testStrictModeDoesntThrowAnExceptionOnLazyLoadingWithSingleModel()
{
EloquentStrictLoadingTestModel1::create();

$models = EloquentStrictLoadingTestModel1::get();

$this->assertInstanceOf(Collection::class, $models);
}

public function testStrictModeDoesntThrowAnExceptionOnAttributes()
{
EloquentStrictLoadingTestModel1::create();
Expand Down Expand Up @@ -85,6 +94,8 @@ public function testStrictModeDoesntThrowAnExceptionOnSingleModelLoading()
{
$model = EloquentStrictLoadingTestModel1::create();

$model = EloquentStrictLoadingTestModel1::find($model->id);

$this->assertInstanceOf(Collection::class, $model->modelTwos);
}

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

$model1 = EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);

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

Expand Down

0 comments on commit f26816d

Please sign in to comment.