Skip to content

Commit

Permalink
Fix nullable migration schema when no required field
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Oct 20, 2017
1 parent 1c8e361 commit 1f55409
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,25 @@ public function handle()
$foreignKeys = $this->processJSONForeignKeys($this->option('fields_from_file'));
}

$validations = trim($this->option('validations'));
if ($this->option('fields_from_file')) {
$validations = $this->processJSONValidations($this->option('fields_from_file'));
}

$fieldsArray = explode(';', $fields);
$fillableArray = [];
$migrationFields = '';

foreach ($fieldsArray as $item) {
$spareParts = explode('#', trim($item));
$fillableArray[] = $spareParts[0];

// Process migration fields
$migrationFields .= $spareParts[0] . '#' . $spareParts[1];
if (!preg_match('/' . $spareParts[0] . '/', $validations)) {
$migrationFields .= '#nullable';
}
$migrationFields .= ';';
}

$commaSeparetedString = implode("', '", $fillableArray);
Expand All @@ -107,14 +120,9 @@ public function handle()
$relationships = $this->processJSONRelationships($this->option('fields_from_file'));
}

$validations = trim($this->option('validations'));
if ($this->option('fields_from_file')) {
$validations = $this->processJSONValidations($this->option('fields_from_file'));
}

$this->call('crud:controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--view-path' => $viewPath, '--route-group' => $routeGroup, '--pagination' => $perPage, '--fields' => $fields, '--validations' => $validations]);
$this->call('crud:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $migrationFields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys]);
$this->call('crud:view', ['name' => $name, '--fields' => $fields, '--validations' => $validations, '--view-path' => $viewPath, '--route-group' => $routeGroup, '--localize' => $localize, '--pk' => $primaryKey]);
if ($localize == 'yes') {
$this->call('crud:lang', ['name' => $name, '--fields' => $fields, '--locales' => $locales]);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/CrudMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function buildClass($name)
$primaryKey = $this->option('pk');

$schemaUp =
"Schema::create('" . $tableName . "', function(Blueprint \$table) {
"Schema::create('" . $tableName . "', function (Blueprint \$table) {
\$table->increments('" . $primaryKey . "');
" . $schemaFields . "\$table->timestamps();
});";
Expand Down

0 comments on commit 1f55409

Please sign in to comment.