From 1f554090c718b5981a7c057ddd3744d85fb39df5 Mon Sep 17 00:00:00 2001 From: Sohel Amin Date: Sat, 21 Oct 2017 00:25:15 +0600 Subject: [PATCH] Fix nullable migration schema when no required field --- src/Commands/CrudCommand.php | 20 ++++++++++++++------ src/Commands/CrudMigrationCommand.php | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Commands/CrudCommand.php b/src/Commands/CrudCommand.php index 7b8cf67..5bc60c9 100644 --- a/src/Commands/CrudCommand.php +++ b/src/Commands/CrudCommand.php @@ -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); @@ -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]); diff --git a/src/Commands/CrudMigrationCommand.php b/src/Commands/CrudMigrationCommand.php index 6c4cbfd..77d7b46 100644 --- a/src/Commands/CrudMigrationCommand.php +++ b/src/Commands/CrudMigrationCommand.php @@ -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(); });";