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] Relax the lazy loading restrictions #37503

Merged
merged 1 commit into from
May 27, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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