Skip to content

Commit

Permalink
Merge pull request #419 from awcodes/fix/column-relationship-query-again
Browse files Browse the repository at this point in the history
Fix: tweaks relationship query for cuartor column
  • Loading branch information
awcodes authored Jan 12, 2024
2 parents c9d2409 + 1e8539c commit ccb90a9
Showing 1 changed file with 4 additions and 33 deletions.
37 changes: 4 additions & 33 deletions src/Components/Tables/CuratorColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,20 @@ public function applyEagerLoading(EloquentBuilder|Relation $query): EloquentBuil
{
$model = $query->getModel();

if ($model instanceof Media || is_subclass_of(Media::class, $model)) {
if (!$this->queriesRelationships($query->getModel())) {
return $query;
}

if (str($this->getName())->contains('.')) {
if (!$this->queriesRelationships($query->getModel())) {
return $query;
}

$relationshipName = $this->getRelationshipName();
} else {
if ($model instanceof Media || is_subclass_of(Media::class, $model)) {
return $query;
}

$relationshipName = $this->getRelationshipName();

if (array_key_exists($relationshipName, $query->getEagerLoads())) {
return $query;
}

return $query->with([$relationshipName]);
}

public function getRelationship(Model $record, ?string $name = null): ?Relation
{
$relationship = null;

if (str($this->getName())->contains('.')) {
foreach (explode('.', $name ?? $this->getRelationshipName()) as $nestedRelationshipName) {
if (!$record->isRelation($nestedRelationshipName)) {
$relationship = null;

break;
}

$relationship = $record->{$nestedRelationshipName}();
$record = $relationship->getRelated();
}
} else {
$nestedRelationshipName = $name ?? $this->getRelationshipName();
if ($record->isRelation($nestedRelationshipName)) {
$relationship = $record->{$nestedRelationshipName}();
}
}

return $relationship;
}
}

0 comments on commit ccb90a9

Please sign in to comment.