From 6ff31ef345b095a679fc0884ea63f3df5dc08a4d Mon Sep 17 00:00:00 2001 From: Andrea Ballarin Date: Mon, 5 Aug 2024 18:54:20 +0200 Subject: [PATCH] timestamp flag on model and input type on input form --- src/Commands/CrudGenerator.php | 4 +- src/Commands/GeneratorCommand.php | 54 ++++++++++++++++++----- src/stubs/Model.stub | 1 + src/stubs/views/bootstrap/form-field.stub | 2 +- src/stubs/views/livewire/form-field.stub | 2 +- src/stubs/views/tailwind/form-field.stub | 2 +- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/Commands/CrudGenerator.php b/src/Commands/CrudGenerator.php index e835c19..b2fbaed 100644 --- a/src/Commands/CrudGenerator.php +++ b/src/Commands/CrudGenerator.php @@ -260,10 +260,10 @@ protected function buildViews(): static $form = "\n"; foreach ($this->getFilteredColumns() as $column) { - $title = Str::title(str_replace('_', ' ', $column)); + $title = Str::title(str_replace('_', ' ', $column['name'])); $tableHead .= $this->getHead($title); - $tableBody .= $this->getBody($column); + $tableBody .= $this->getBody($column['name']); $viewRows .= $this->getField($title, $column, 'view-field'); $form .= $this->getField($title, $column); } diff --git a/src/Commands/GeneratorCommand.php b/src/Commands/GeneratorCommand.php index 338feda..640846d 100644 --- a/src/Commands/GeneratorCommand.php +++ b/src/Commands/GeneratorCommand.php @@ -320,8 +320,9 @@ protected function getField($title, $column, string $type = 'form-field'): strin { $replace = array_merge($this->buildReplacements(), [ '{{title}}' => $title, - '{{column}}' => $column, - '{{column_snake}}' => Str::snake($column), + '{{column}}' => $column['name'], + '{{column_snake}}' => Str::snake($column['name']), + '{{column_type}}' => $this->getHtmlInputType($column['type_name']), ]); return str_replace( @@ -427,17 +428,38 @@ protected function getColumns(): ?array protected function getFilteredColumns(): array { $unwanted = $this->unwantedColumns; - $columns = []; - - foreach ($this->getColumns() as $column) { - $columns[] = $column['name']; - } + $columns = $this->getColumns(); - return array_filter($columns, function ($value) use ($unwanted) { - return ! in_array($value, $unwanted); + return array_filter($columns, function ($column) use ($unwanted) { + return ! in_array($column['name'], $unwanted); }); } + protected function getHtmlInputType(string $columnType): string + { + $mapping = [ + 'string' => 'text', + 'text' => 'textarea', + 'longtext' => 'textarea', + 'integer' => 'number', + 'smallint' => 'number', + 'bigint' => 'number', + 'decimal' => 'number', + 'float' => 'number', + 'datetime' => 'datetime-local', + 'timestamp' => 'datetime-local', + 'date' => 'date', + 'time' => 'time', + 'boolean' => 'checkbox', + 'blob' => 'file', + 'bytea' => 'file', + 'varbinary' => 'file', + 'binary' => 'file', + 'enum' => 'select', + // Add other mappings as necessary + ]; + return $mapping[$columnType] ?? 'text'; + } /** * Make model attributes/replacements. * @@ -500,7 +522,7 @@ protected function modelReplacements(): array // Add quotes to the unwanted columns for fillable array_walk($filterColumns, function (&$value) { - $value = "'".$value."'"; + $value = "'".$value['name']."'"; }); // CSV format @@ -511,6 +533,8 @@ protected function modelReplacements(): array [$relations, $properties] = (new ModelGenerator($this->table, $properties, $this->modelNamespace))->getEloquentRelations(); + $timestampColumns = ($this->requireTimestamp()) ? 'true' : 'false'; + return [ '{{fillable}}' => $fillable(), '{{rules}}' => $rules(), @@ -520,8 +544,18 @@ protected function modelReplacements(): array '{{softDeletes}}' => $softDeletes, '{{livewireFormProperties}}' => $livewireFormProperties, '{{livewireFormSetValues}}' => $livewireFormSetValues, + '{{timestampColumns}}' => $timestampColumns, ]; } + /** + * Get the desired class name from the input. + * + * @return bool + */ + protected function requireTimestamp(): bool + { + return Schema::hasColumn($this->table, 'created_at') && Schema::hasColumn($this->table, 'updated_at'); + } /** * Get the desired class name from the input. diff --git a/src/stubs/Model.stub b/src/stubs/Model.stub index 9626075..aecafee 100644 --- a/src/stubs/Model.stub +++ b/src/stubs/Model.stub @@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Model; class {{modelName}} extends Model { {{softDeletes}} + public $timestamps = {{timestampColumns}}; protected $perPage = 20; /** diff --git a/src/stubs/views/bootstrap/form-field.stub b/src/stubs/views/bootstrap/form-field.stub index c8a7697..ba4ecd3 100644 --- a/src/stubs/views/bootstrap/form-field.stub +++ b/src/stubs/views/bootstrap/form-field.stub @@ -1,5 +1,5 @@
- + {!! $errors->first('{{column}}', '') !!}
diff --git a/src/stubs/views/livewire/form-field.stub b/src/stubs/views/livewire/form-field.stub index 719a82f..9110eb1 100644 --- a/src/stubs/views/livewire/form-field.stub +++ b/src/stubs/views/livewire/form-field.stub @@ -1,6 +1,6 @@
- + @error('form.{{column}}') @enderror diff --git a/src/stubs/views/tailwind/form-field.stub b/src/stubs/views/tailwind/form-field.stub index e65450d..61d5049 100644 --- a/src/stubs/views/tailwind/form-field.stub +++ b/src/stubs/views/tailwind/form-field.stub @@ -1,5 +1,5 @@
- +