Skip to content

Commit

Permalink
Soft deletes changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Dec 16, 2017
1 parent 55a2804 commit f9c9d43
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
7 changes: 4 additions & 3 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +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 |
| `--soft-deletes` | Include soft deletes fields. eg. `--soft-deletes=yes` |


### Controller Options:
Expand Down Expand Up @@ -59,7 +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 |
| `--soft-deletes` | Include soft deletes fields. eg. `--soft-deletes=yes` |

### Migration Options:

Expand All @@ -69,7 +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 |
| `--soft-deletes` | Include soft deletes fields. eg. `--soft-deletes=yes` |

### Lang Options:

Expand All @@ -94,6 +94,7 @@
| `--relationships` | The relationships for the model. e.g. `--relationships="comments#hasMany#App\Comment"` in the format |
| `--route` | Include Crud route to routes.php? yes or no |
| `--route-group` | Prefix of the route group |
| `--soft-deletes` | Include soft deletes fields. eg. `--soft-deletes=yes` |

### API Controller Options:

Expand Down
9 changes: 6 additions & 3 deletions src/Commands/CrudApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class CrudApiCommand extends Command
{--foreign-keys= : The foreign keys for the table.}
{--relationships= : The relationships for the model.}
{--route=yes : Include Crud route to routes.php? yes|no.}
{--route-group= : Prefix of the route group.}';
{--route-group= : Prefix of the route group.}
{--soft-deletes=no : Include soft deletes fields.}';

/**
* The console command description.
Expand Down Expand Up @@ -105,9 +106,11 @@ public function handle()
$validations = $this->processJSONValidations($this->option('fields_from_file'));
}

$softDeletes = $this->option('soft-deletes');

$this->call('crud:api-controller', ['name' => $controllerNamespace . $name . 'Controller', '--crud-name' => $name, '--model-name' => $modelName, '--model-namespace' => $modelNamespace, '--pagination' => $perPage, '--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:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships, '--soft-deletes' => $softDeletes]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $fields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys, '--soft-deletes' => $softDeletes]);

// For optimizing the class loader
$this->callSilent('optimize');
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CrudCommand extends Command
{--form-helper=html : Helper for generating the form.}
{--localize=no : Allow to localize? yes|no.}
{--locales=en : Locales language type.}
{--soft-deletes : Include soft deletes fields.}';
{--soft-deletes=no : Include soft deletes fields.}';

/**
* The console command description.
Expand Down Expand Up @@ -123,10 +123,11 @@ public function handle()
}

$formHelper = $this->option('form-helper');
$softDeletes = $this->option('soft-deletes');

$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, '--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:model', ['name' => $modelNamespace . $modelName, '--fillable' => $fillable, '--table' => $tableName, '--pk' => $primaryKey, '--relationships' => $relationships, '--soft-deletes' => $softDeletes]);
$this->call('crud:migration', ['name' => $migrationName, '--schema' => $migrationFields, '--pk' => $primaryKey, '--indexes' => $indexes, '--foreign-keys' => $foreignKeys, '--soft-deletes' => $softDeletes]);
$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]);
Expand Down
11 changes: 6 additions & 5 deletions src/Commands/CrudMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CrudMigrationCommand extends GeneratorCommand
{--indexes= : The fields to add an index to.}
{--foreign-keys= : Foreign keys.}
{--pk=id : The name of the primary key.}
{--soft-deletes : Include soft deletes fields.}';
{--soft-deletes=no : Include soft deletes fields.}';

/**
* The console command description.
Expand Down Expand Up @@ -209,17 +209,18 @@ protected function buildClass($name)
}

$primaryKey = $this->option('pk');
$softDeletes = $this->option('soft-deletes');

$softDeletes = '';
if ($this->option('soft-deletes')) {
$softDeletes = "\$table->softDeletes();\n" . $tabIndent . $tabIndent . $tabIndent;
$softDeletesSnippets = '';
if ($softDeletes == 'yes') {
$softDeletesSnippets = "\$table->softDeletes();\n" . $tabIndent . $tabIndent . $tabIndent;
}

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

Expand Down
11 changes: 6 additions & 5 deletions src/Commands/CrudModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CrudModelCommand extends GeneratorCommand
{--fillable= : The names of the fillable columns.}
{--relationships= : The relationships for the model}
{--pk=id : The name of the primary key.}
{--soft-deletes : Include soft deletes fields.}';
{--soft-deletes=no : Include soft deletes fields.}';

/**
* The console command description.
Expand Down Expand Up @@ -71,6 +71,7 @@ protected function buildClass($name)
$fillable = $this->option('fillable');
$primaryKey = $this->option('pk');
$relationships = trim($this->option('relationships')) != '' ? explode(',', trim($this->option('relationships'))) : [];
$softDeletes = $this->option('soft-deletes');

if (!empty($primaryKey)) {
$primaryKey = <<<EOD
Expand All @@ -87,7 +88,7 @@ protected function buildClass($name)
->replaceTable($stub, $table)
->replaceFillable($stub, $fillable)
->replacePrimaryKey($stub, $primaryKey)
->replaceSoftDelete($stub, $this->option('soft-deletes'));
->replaceSoftDelete($stub, $softDeletes);

foreach ($relationships as $rel) {
// relationshipname#relationshiptype#args_separated_by_pipes
Expand Down Expand Up @@ -169,13 +170,13 @@ protected function replacePrimaryKey(&$stub, $primaryKey)
* Replace the (optional) soft deletes part for the given stub.
*
* @param string $stub
* @param boolean $replaceSoftDelete
* @param string $replaceSoftDelete
*
* @return $this
*/
protected function replaceSoftDelete(&$stub, $replaceSoftDelete = false)
protected function replaceSoftDelete(&$stub, $replaceSoftDelete)
{
if ($replaceSoftDelete) {
if ($replaceSoftDelete == 'yes') {
$stub = str_replace('{{softDeletes}}', "use SoftDeletes;\n ", $stub);
$stub = str_replace('{{useSoftDeletes}}', "use Illuminate\Database\Eloquent\SoftDeletes;\n", $stub);
} else {
Expand Down

0 comments on commit f9c9d43

Please sign in to comment.