diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index fb1715d40b..694da3bc4e 100644 --- a/packages/support/src/Concerns/HasCellState.php +++ b/packages/support/src/Concerns/HasCellState.php @@ -24,6 +24,13 @@ trait HasCellState protected ?string $inverseRelationshipName = null; + protected array $cachedStatePerRecordId = []; + + protected function getRecordId(): mixed + { + return $this->getRecord()?->getKey(); + } + public function inverseRelationship(?string $name): static { $this->inverseRelationshipName = $name; @@ -75,6 +82,10 @@ trait HasCellState return null; } + if (array_key_exists($this->getRecordId(), $this->cachedStatePerRecordId)) { + return $this->cachedStatePerRecordId[$this->getRecordId()]; + } + $state = ($this->getStateUsing !== null) ? $this->evaluate($this->getStateUsing) : $this->getStateFromRecord(); @@ -90,6 +101,8 @@ trait HasCellState $state = $this->getDefaultState(); } + $this->cachedStatePerRecordId[$this->getRecordId()] = $state; + return $state; }