From 8cdefd3550427c24780e776404b4bfd615b9d7da Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Thu, 2 Jan 2025 23:12:18 +0545 Subject: [PATCH] Literal added to generate the Enum path --- .../Concerns/CanGenerateModelForms.php | 29 ++++++++++++------- .../Commands/Concerns/CanReadModelSchemas.php | 7 +++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 9d0766e9fd..71f5277b2b 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -95,19 +95,28 @@ public function getFormComponents(?string $model = null, array $exceptColumns = default => TextInput::class, }; - if (isset($type['name']) && $type['name'] === 'enum') { + $enumCasts = $this->getEnumCasts($model); + + if (isset($type['name']) && ($type['name'] === 'enum' || array_key_exists($componentName, $enumCasts))) { $componentData['type'] = Select::class; - $options = array_combine( - $type['values'], - array_map( - fn ($value) => (string) str($value) - ->kebab() - ->replace(['-', '_'], ' ') - ->ucfirst(), - $type['values'] + + if (array_key_exists($componentName, $enumCasts)) { + $options = $enumCasts[$componentName]; + $componentData['options'] = [new Literal("\\{$options}::class")]; + } else { + $options = array_combine( + $type['values'], + array_map( + fn($value) => (string)str($value) + ->kebab() + ->replace(['-', '_'], ' ') + ->ucfirst(), + $type['values'] ) ); - $componentData['options'] = [$options]; + $componentData['options'] = [$options]; + } + if ($column['default']) { $componentData['default'] = [$this->parseDefaultExpression($column, $model)]; } diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index 3e5e28b2a0..955d8eafba 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -9,6 +9,7 @@ use Illuminate\Support\Str; use ReflectionClass; use ReflectionException; +use function Illuminate\Support\enum_value; trait CanReadModelSchemas { @@ -229,4 +230,10 @@ public function getRecordTitleAttribute(): ?string { return null; } + + protected function getEnumCasts($model) + { + $casts = app($model)->getCasts(); + return array_filter($casts, fn ($cast) => enum_exists($cast)); + } }