diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index 6a2f775d97..7c4f7dbc83 100644 --- a/packages/support/src/Concerns/HasCellState.php +++ b/packages/support/src/Concerns/HasCellState.php @@ -103,6 +103,44 @@ trait HasCellState public function getStateFromRecord(): mixed { $record = $this->getRecord(); + + if ($record instanceof Model) { + $relationship = $this->getRelationship($record); + + if ($relationship) { + $relationshipAttribute = $this->getFullAttributeName($record); + + $state = collect($this->getRelationshipResults($record)) + ->reduce( + function (Collection $carry, Model $record) use ($relationshipAttribute): Collection { + if ( + ($record instanceof HasRichContent) && + $record->hasRichContentAttribute($relationshipAttribute) + ) { + $state = $record->getRichContentAttribute($relationshipAttribute); + } else { + $state = data_get($record, $relationshipAttribute); + } + + if (blank($state)) { + return $carry; + } + + return $carry->push($state); + }, + initial: collect(), + ) + ->when($this->isDistinctList(), fn (Collection $state) => $state->unique()) + ->values(); + + if (! $state->count()) { + return null; + } + + return $state->all(); + } + } + $name = $this->getName(); if ( @@ -114,39 +152,7 @@ trait HasCellState $state = data_get($record, $name); } - if ($state !== null) { - return $state; - } - - if (($this instanceof Column) && is_array($record)) { /** @phpstan-ignore function.impossibleType, booleanAnd.alwaysFalse */ - return null; - } - - if (! $this->hasRelationship($record)) { - return null; - } - - $relationship = $this->getRelationship($record); - - if (! $relationship) { - return null; - } - - $attributeName = $this->getAttributeName($record); - $fullAttributeName = $this->getFullAttributeName($record); - - $state = collect($this->getRelationshipResults($record)) - ->filter(fn (Model $record): bool => array_key_exists($attributeName, $record->attributesToArray())) - ->pluck($fullAttributeName) - ->filter(fn ($state): bool => filled($state)) - ->when($this->isDistinctList(), fn (Collection $state) => $state->unique()) - ->values(); - - if (! $state->count()) { - return null; - } - - return $state->all(); + return $state; } public function clearCachedState(): void