Skip to content

Commit

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

Fix eager load nested relationships
  • Loading branch information
tabacitu authored Mar 8, 2021
2 parents b53d3aa + d5e03d7 commit 90cf9d0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/app/Library/CrudPanel/Traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Backpack\CRUD\app\Library\CrudPanel\Traits;

use Exception;

/**
* Properties and methods used by the List operation.
*/
Expand Down Expand Up @@ -82,8 +84,22 @@ public function autoEagerLoadRelationshipColumns()
{
$relationships = $this->getColumnsRelationships();

if (count($relationships)) {
$this->with($relationships);
foreach ($relationships as $relation) {
if (strpos($relation, '.') !== false) {
$parts = explode('.', $relation);
$model = $this->model;

// 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 {
$model = $model->$part()->getRelated();
} catch (Exception $e) {
$relation = join('.', array_slice($parts, 0, $i));
}
}
}
$this->with($relation);
}
}

Expand Down

0 comments on commit 90cf9d0

Please sign in to comment.