diff --git a/doc/options.md b/doc/options.md index 72a95e9..21c5069 100644 --- a/doc/options.md +++ b/doc/options.md @@ -20,6 +20,7 @@ | `--form-helper` | Helper for the form. eg. `--form-helper=html`, `--form-helper=laravelcollective` | | `--localize` | Allow to localize. e.g. localize=yes | | `--locales` | Locales language type. e.g. locals=en | +| `--soft-deletes` | Include soft deletes fields | ### Controller Options: @@ -58,6 +59,7 @@ | `--fillable` | The name of the view path | | `--relationships` | The relationships for the model. e.g. `--relationships="comments#hasMany#App\Comment"` in the format | | `--pk` | The name of the primary key | +| `--soft-deletes` | Include soft deletes fields | ### Migration Options: @@ -67,6 +69,7 @@ | `--indexes` | The fields to add an index to. append "#unique" to a field name to add a unique index. Create composite fields by separating fieldnames with a pipe (` --indexes="title,field1|field2#unique" ` will create normal index on title, and unique composite on fld1 and fld2) | | `--foreign-keys` | Any foreign keys for the table. e.g. `--foreign-keys="user_id#id#users#cascade"` where user_id is the column name, id is the name of the field on the foreign table, users is the name of the foreign table, and cascade is the operation 'ON DELETE' together with 'ON UPDATE' | | `--pk` | The name of the primary key | +| `--soft-deletes` | Include soft deletes fields | ### Lang Options: diff --git a/src/Commands/CrudCommand.php b/src/Commands/CrudCommand.php index b24e402..b201907 100644 --- a/src/Commands/CrudCommand.php +++ b/src/Commands/CrudCommand.php @@ -29,7 +29,8 @@ class CrudCommand extends Command {--view-path= : The name of the view path.} {--form-helper=html : Helper for generating the form.} {--localize=no : Allow to localize? yes|no.} - {--locales=en : Locales language type.}'; + {--locales=en : Locales language type.} + {--soft-deletes : Include soft deletes fields.}'; /** * The console command description. @@ -124,8 +125,8 @@ public function handle() $formHelper = $this->option('form-helper'); $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' => $migrationFields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys]); + $this->call('crud:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships, '--soft-deletes' => $this->option('soft-deletes')]); + $this->call('crud:migration', ['name' => $migrationName, '--schema' => $migrationFields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys, '--soft-deletes' => $this->option('soft-deletes')]); $this->call('crud:view', ['name' => $name, '--fields' => $fields, '--validations' => $validations, '--view-path' => $viewPath, '--route-group' => $routeGroup, '--localize' => $localize, '--pk' => $primaryKey, '--form-helper' => $formHelper]); 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 b642940..dbf86f5 100644 --- a/src/Commands/CrudMigrationCommand.php +++ b/src/Commands/CrudMigrationCommand.php @@ -16,7 +16,8 @@ class CrudMigrationCommand extends GeneratorCommand {--schema= : The name of the schema.} {--indexes= : The fields to add an index to.} {--foreign-keys= : Foreign keys.} - {--pk=id : The name of the primary key.}'; + {--pk=id : The name of the primary key.} + {--soft-deletes : Include soft deletes fields.}'; /** * The console command description. @@ -209,11 +210,18 @@ protected function buildClass($name) $primaryKey = $this->option('pk'); + $softDeletes = ''; + if ($this->option('soft-deletes')) { + $softDeletes = "\$table->softDeletes();\n" . $tabIndent . $tabIndent . $tabIndent; + } + $schemaUp = "Schema::create('" . $tableName . "', function (Blueprint \$table) { \$table->increments('" . $primaryKey . "'); - " . $schemaFields . "\$table->timestamps(); - });"; + \$table->timestamps();\n" . $tabIndent . $tabIndent . $tabIndent . + $softDeletes . + $schemaFields . + "});"; $schemaDown = "Schema::drop('" . $tableName . "');"; diff --git a/src/Commands/CrudModelCommand.php b/src/Commands/CrudModelCommand.php index 99f2873..b084622 100644 --- a/src/Commands/CrudModelCommand.php +++ b/src/Commands/CrudModelCommand.php @@ -16,7 +16,8 @@ class CrudModelCommand extends GeneratorCommand {--table= : The name of the table.} {--fillable= : The names of the fillable columns.} {--relationships= : The relationships for the model} - {--pk=id : The name of the primary key.}'; + {--pk=id : The name of the primary key.} + {--soft-deletes : Include soft deletes fields.}'; /** * The console command description. @@ -85,7 +86,8 @@ protected function buildClass($name) $ret = $this->replaceNamespace($stub, $name) ->replaceTable($stub, $table) ->replaceFillable($stub, $fillable) - ->replacePrimaryKey($stub, $primaryKey); + ->replacePrimaryKey($stub, $primaryKey) + ->replaceSoftDelete($stub, $this->option('soft-deletes')); foreach ($relationships as $rel) { // relationshipname#relationshiptype#args_separated_by_pipes @@ -163,6 +165,27 @@ protected function replacePrimaryKey(&$stub, $primaryKey) return $this; } + /** + * Replace the (optional) soft deletes part for the given stub. + * + * @param string $stub + * @param boolean $replaceSoftDelete + * + * @return $this + */ + protected function replaceSoftDelete(&$stub, $replaceSoftDelete = false) + { + if ($replaceSoftDelete) { + $stub = str_replace('{{softDeletes}}', "use SoftDeletes;\n ", $stub); + $stub = str_replace('{{useSoftDeletes}}', "use Illuminate\Database\Eloquent\SoftDeletes;\n", $stub); + } else { + $stub = str_replace('{{softDeletes}}', '', $stub); + $stub = str_replace('{{useSoftDeletes}}', '', $stub); + } + + return $this; + } + /** * Create the code for a model relationship * diff --git a/src/stubs/model.stub b/src/stubs/model.stub index 4d69dfd..24a106c 100644 --- a/src/stubs/model.stub +++ b/src/stubs/model.stub @@ -3,10 +3,10 @@ namespace DummyNamespace; use Illuminate\Database\Eloquent\Model; - +{{useSoftDeletes}} class DummyClass extends Model { - /** + {{softDeletes}}/** * The database table used by the model. * * @var string