Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timestamp flag on model and input type on input forms #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Commands/CrudGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
54 changes: 44 additions & 10 deletions src/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/stubs/Model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Model;
class {{modelName}} extends Model
{
{{softDeletes}}
public $timestamps = {{timestampColumns}};
protected $perPage = 20;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/views/bootstrap/form-field.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="form-group mb-2 mb20">
<label for="{{column_snake}}" class="form-label">{{ __('{{title}}') }}</label>
<input type="text" name="{{column}}" class="form-control @error('{{column}}') is-invalid @enderror" value="{{ old('{{column}}', ${{modelNameLowerCase}}?->{{column}}) }}" id="{{column_snake}}" placeholder="{{title}}">
<input type="{{column_type}}" name="{{column}}" class="form-control @error('{{column}}') is-invalid @enderror" value="{{ old('{{column}}', ${{modelNameLowerCase}}?->{{column}}) }}" id="{{column_snake}}" placeholder="{{title}}">
{!! $errors->first('{{column}}', '<div class="invalid-feedback" role="alert"><strong>:message</strong></div>') !!}
</div>
2 changes: 1 addition & 1 deletion src/stubs/views/livewire/form-field.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<x-input-label for="{{column_snake}}" :value="__('{{title}}')"/>
<x-text-input wire:model="form.{{column}}" id="{{column_snake}}" name="{{column}}" type="text" class="mt-1 block w-full" autocomplete="{{column}}" placeholder="{{title}}"/>
<x-{{column_type}}-input wire:model="form.{{column}}" id="{{column_snake}}" name="{{column}}" type="text" class="mt-1 block w-full" autocomplete="{{column}}" placeholder="{{title}}"/>
@error('form.{{column}}')
<x-input-error class="mt-2" :messages="$message"/>
@enderror
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/views/tailwind/form-field.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
<x-input-label for="{{column_snake}}" :value="__('{{title}}')"/>
<x-text-input id="{{column_snake}}" name="{{column}}" type="text" class="mt-1 block w-full" :value="old('{{column}}', ${{modelNameLowerCase}}?->{{column}})" autocomplete="{{column}}" placeholder="{{title}}"/>
<x-{{column_type}}-input id="{{column_snake}}" name="{{column}}" type="text" class="mt-1 block w-full" :value="old('{{column}}', ${{modelNameLowerCase}}?->{{column}})" autocomplete="{{column}}" placeholder="{{title}}"/>
<x-input-error class="mt-2" :messages="$errors->get('{{column}}')"/>
</div>