Skip to content

Commit

Permalink
Merge pull request #3602 from Laravel-Backpack/fix-eager-load-nested-…
Browse files Browse the repository at this point in the history
…relationships-promatik

Fix eager load nested relationships - refactor loop to check nested relations
  • Loading branch information
pxpm authored Mar 8, 2021
2 parents 62a77ad + 537ed80 commit d5e03d7
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/app/Library/CrudPanel/Traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,17 @@ public function autoEagerLoadRelationshipColumns()

foreach ($relationships as $relation) {
if (strpos($relation, '.') !== false) {
$relation_parts = explode('.', $relation);
$last_relation_part = $last_valid_relation_method = end($relation_parts);
$parts = explode('.', $relation);
$model = $this->model;

array_reduce(array_splice($relation_parts, 0, count($relation_parts)), function ($obj, $method) use (&$last_valid_relation_method) {
// Iterate over each relation part to find the valid relations without attributes
// We should eager load the relation but not the attribute
foreach ($parts as $i => $part) {
try {
$result = $obj->$method();
$last_valid_relation_method = $method;

return $result->getRelated();
$model = $model->$part()->getRelated();
} catch (Exception $e) {
return;
$relation = join('.', array_slice($parts, 0, $i));
}
}, $this->model);

// when this keys don't match means the last part of the relation string is the attribute in the relation and
// not a nested relation. In that case, we should eager load the relation but not the attribute
if ($last_valid_relation_method != $last_relation_part) {
// remove the last part of the relation string because it is the attribute in the relationship
$relation = substr($relation, 0, strrpos($relation, '.'));
}
}
$this->with($relation);
Expand Down

0 comments on commit d5e03d7

Please sign in to comment.