Skip to content

Commit

Permalink
Enum Type field added
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Jun 15, 2016
1 parent 2ea600f commit cfdfc0e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
28 changes: 28 additions & 0 deletions publish/EnumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App;

trait EnumTrait
{
/**
* Get enum values from a column.
*
* @param string $column
*
* @return array
*/
public static function getEnumValues($column)
{
$instance = new static;
$type = DB::select(DB::raw('SHOW COLUMNS FROM ' . $instance->getTable() . ' WHERE Field = "' . $column . '"'))[0]->Type;
preg_match('/^enum\((.*)\)$/', $type, $matches);

$enum = [];
foreach (explode(',', $matches[1]) as $value) {
$v = trim($value, "'");
$enum = array_add($enum, $v, $v);
}

return $enum;
}
}
4 changes: 2 additions & 2 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CrudCommand extends Command
{--view-path= : The name of the view path.}
{--namespace= : Namespace of the controller.}
{--route-group= : Prefix of the route group.}
{--localize=yes : Localize the generated files? yes|no. }
{--localize=no : Localize the generated files? yes|no. }
{--locales=en : Locales to create lang files for.}';

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ public function handle()
$this->call('crud:model', ['name' => $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey]);
$this->call('crud:view', ['name' => $name, '--fields' => $fields, '--view-path' => $viewPath, '--route-group' => $routeGroup, '--localize' => $localize, '--pk' => $primaryKey]);
if ($this->option('localize') == 'yes') {
if ($localize == 'yes') {
$this->call('crud:lang', ['name' => $name, '--fields' => $fields, '--locales' => $locales]);
}
// For optimizing the class loader
Expand Down
4 changes: 4 additions & 0 deletions src/Commands/CrudMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ protected function buildClass($name)
$schemaFields .= "\$table->float('" . $item['name'] . "');\n" . $tabIndent . $tabIndent . $tabIndent;
break;

case 'enum':
$schemaFields .= "\$table->enum('" . $item['name'] . "', []);\n" . $tabIndent . $tabIndent . $tabIndent;
break;

default:
$schemaFields .= "\$table->string('" . $item['name'] . "');\n" . $tabIndent . $tabIndent . $tabIndent;
break;
Expand Down
23 changes: 22 additions & 1 deletion src/Commands/CrudViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CrudViewCommand extends Command
{--view-path= : The name of the view path.}
{--route-group= : Prefix of the route group.}
{--pk=id : The name of the primary key.}
{--localize=yes : Localize the view? yes|no.}';
{--localize=no : Localize the view? yes|no.}';

/**
* The console command description.
Expand Down Expand Up @@ -65,6 +65,7 @@ class CrudViewCommand extends Command
'timestamp' => 'datetime-local',
'time' => 'time',
'boolean' => 'radio',
'enum' => 'select',
];

/**
Expand Down Expand Up @@ -407,6 +408,9 @@ protected function createField($item)
case 'radio':
return $this->createRadioField($item);
break;
case 'select':
return $this->createSelectField($item);
break;
default: // text
return $this->createFormField($item);
}
Expand Down Expand Up @@ -484,4 +488,21 @@ protected function createRadioField($item)

return $this->wrapField($item, sprintf($field, $item['name']));
}

/**
* Create a select field using the form helper.
*
* @param string $item
*
* @return string
*/
protected function createSelectField($item)
{
$required = ($item['required'] === true) ? ", 'required' => 'required'" : "";

return $this->wrapField(
$item,
"{!! Form::select('" . $item['name'] . "', [], null, ['class' => 'form-control'$required]) !!}"
);
}
}
4 changes: 4 additions & 0 deletions src/CrudGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function boot()
__DIR__ . '/../publish/app.blade.php' => base_path('resources/views/layouts/app.blade.php'),
]);

$this->publishes([
__DIR__ . '/../publish/EnumTrait.php' => app_path(),
]);

$this->publishes([
__DIR__ . '/stubs/' => base_path('resources/crud-generator/'),
]);
Expand Down

0 comments on commit cfdfc0e

Please sign in to comment.