Skip to content

Commit 5548f95

Browse files
authored
[12.x] Fix for global automaticallyEagerLoadRelationships not working in certain cases (#55443)
1 parent e7ad734 commit 5548f95

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -854,13 +854,9 @@ public function get($columns = ['*'])
854854
$models = $builder->eagerLoadRelations($models);
855855
}
856856

857-
$collection = $builder->getModel()->newCollection($models);
858-
859-
if (Model::isAutomaticallyEagerLoadingRelationships()) {
860-
$collection->withRelationshipAutoloading();
861-
}
862-
863-
return $this->applyAfterQueryCallbacks($collection);
857+
return $this->applyAfterQueryCallbacks(
858+
$builder->getModel()->newCollection($models)
859+
);
864860
}
865861

866862
/**

src/Illuminate/Database/Eloquent/HasCollection.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public function newCollection(array $models = [])
2727
{
2828
static::$resolvedCollectionClasses[static::class] ??= ($this->resolveCollectionFromAttribute() ?? static::$collectionClass);
2929

30-
return new static::$resolvedCollectionClasses[static::class]($models);
30+
$collection = new static::$resolvedCollectionClasses[static::class]($models);
31+
32+
if (Model::isAutomaticallyEagerLoadingRelationships()) {
33+
$collection->withRelationshipAutoloading();
34+
}
35+
36+
return $collection;
3137
}
3238

3339
/**

0 commit comments

Comments
 (0)