Skip to content

[12.x] Added Automatic Relation Loading (Eager Loading) Feature #53655

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

Merged
merged 13 commits into from
Apr 8, 2025
Prev Previous commit
formatting
  • Loading branch information
taylorotwell committed Apr 8, 2025
commit a346fc89d2a02132676b815b5dd630ccb514f6cf
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ public function get($columns = ['*'])

$collection = $builder->getModel()->newCollection($models);

if (Model::isAutoloadingRelationsGlobally()) {
if (Model::isAutomaticallyEagerLoadingRelationships()) {
$collection->withRelationshipAutoloading();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ public function loadMissing($relations)
/**
* Load a relationship path for models of the given type if it is not already eager loaded.
*
* @param array $tuples
* @param array<int, <string, class-string>> $tuples
* @return void
*/
public function loadMissingRelationsViaRelationAndClassTuples(array $tuples)
public function loadMissingRelationshipChain(array $tuples)
{
[$relation, $class] = array_shift($tuples);

Expand All @@ -274,7 +274,7 @@ public function loadMissingRelationsViaRelationAndClassTuples(array $tuples)
$models = $models->collapse();
}

(new static($models))->loadMissingRelationsViaRelationAndClassTuples($tuples);
(new static($models))->loadMissingRelationshipChain($tuples);
}

/**
Expand Down Expand Up @@ -757,7 +757,7 @@ protected function duplicateComparator($strict)
*/
public function withRelationshipAutoloading()
{
$callback = fn ($tuples) => $this->loadMissingRelationsViaRelationAndClassTuples($tuples);
$callback = fn ($tuples) => $this->loadMissingRelationshipChain($tuples);

foreach ($this as $model) {
if (! $model->hasRelationAutoloadCallback()) {
Expand Down
16 changes: 8 additions & 8 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
protected static $modelsShouldPreventLazyLoading = false;

/**
* Indicates whether relations should be automatically loaded on all models.
* Indicates whether relations should be automatically loaded on all models when they are accessed.
*
* @var bool
*/
protected static $modelsShouldGlobalAutoloadRelations = false;
protected static $modelsShouldAutomaticallyEagerLoadRelationships = false;

/**
* The callback that is responsible for handling lazy loading violations.
Expand Down Expand Up @@ -454,14 +454,14 @@ public static function preventLazyLoading($value = true)
}

/**
* Determine if model relationships should be automatically loaded.
* Determine if model relationships should be automatically eager loaded when accessed.
*
* @param bool $value
* @return void
*/
public static function globalAutoloadRelations($value = true)
public static function automaticallyEagerLoadRelationships($value = true)
{
static::$modelsShouldGlobalAutoloadRelations = $value;
static::$modelsShouldAutomaticallyEagerLoadRelationships = $value;
}

/**
Expand Down Expand Up @@ -2250,13 +2250,13 @@ public static function preventsLazyLoading()
}

/**
* Determine if relations autoload is enabled.
* Determine if relationships are being automatically eager loaded when accessed.
*
* @return bool
*/
public static function isAutoloadingRelationsGlobally()
public static function isAutomaticallyEagerLoadingRelationships()
{
return static::$modelsShouldGlobalAutoloadRelations;
return static::$modelsShouldAutomaticallyEagerLoadRelationships;
}

/**
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy