Skip to content

Commit

Permalink
Merge pull request #204 from raulneis/master
Browse files Browse the repository at this point in the history
Parameter --soft-deletes to include soft deletes fields
  • Loading branch information
sohelamin authored Dec 16, 2017
2 parents 6038689 + 98fe051 commit 55a2804
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
3 changes: 3 additions & 0 deletions doc/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:

Expand All @@ -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:

Expand Down
7 changes: 4 additions & 3 deletions src/Commands/CrudCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
Expand Down
14 changes: 11 additions & 3 deletions src/Commands/CrudMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 . "');";

Expand Down
27 changes: 25 additions & 2 deletions src/Commands/CrudModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions src/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55a2804

Please sign in to comment.