diff --git a/src/SelectTree.php b/src/SelectTree.php index e809057..8a9b7b7 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -301,7 +301,13 @@ public function multiple(Closure|bool $multiple = true): static public function prepend(Closure|array|null $prepend = null): static { - $this->prepend = $prepend; + $this->prepend = $this->evaluate($prepend); + + if (is_array($this->prepend) && isset($this->prepend['name'], $this->prepend['value'])) { + $this->prepend['value'] = (string) $this->prepend['value']; + } else { + throw new \InvalidArgumentException('The provided prepend value must be an array with "name" and "value" keys.'); + } return $this; } @@ -441,9 +447,11 @@ public function getIndependent(): bool return $this->evaluate($this->independent); } - public function getCustomKey($record) + public function getCustomKey($record): string { - return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + $key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey}; + + return (string) $key; } public function getWithCount(): bool @@ -614,4 +622,15 @@ public function createOptionModalHeading(string|Closure|null $heading): static return $this; } + + public function getState(): mixed + { + $state = parent::getState(); + + if (is_array($state)) { + return array_map(fn ($value) => (string) $value, $state); + } + + return (string) $state; + } }