diff --git a/app/Console/Commands/MakeClass.php b/app/Console/Commands/MakeClass.php index a4ea33a9a455..9779c79e5f99 100644 --- a/app/Console/Commands/MakeClass.php +++ b/app/Console/Commands/MakeClass.php @@ -41,6 +41,19 @@ class MakeClass extends GeneratorCommand ]; } + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return array( + array('fields', null, InputOption::VALUE_OPTIONAL, 'The model attributes.', null), + ); + } + + public function getTemplateContents() { $module = $this->laravel['modules']->findOrFail($this->getModuleName()); @@ -51,6 +64,7 @@ class MakeClass extends GeneratorCommand 'LOWER_NAME' => $module->getLowerName(), 'CLASS' => $this->getClass(), 'STUDLY_NAME' => Str::studly($module->getLowerName()), + 'COLUMNS' => $this->getColumns(), ]))->render(); } @@ -70,4 +84,23 @@ class MakeClass extends GeneratorCommand return studly_case($this->argument('prefix')) . studly_case($this->argument('name')) . Str::studly($this->argument('class')); } + protected function getColumns() + { + $fields = $this->option('fields'); + $fields = explode(',', $fields); + $str = ''; + + foreach ($fields as $field) { + $field = explode(':', $field)[0]; + $str .= '[ + \''. $field . '\', + function ($model) { + return $model->' . $field . '; + } + ],'; + } + + return $str; + } + } diff --git a/app/Console/Commands/MakeModule.php b/app/Console/Commands/MakeModule.php index ce1a310adbea..f2e681e8ab48 100644 --- a/app/Console/Commands/MakeModule.php +++ b/app/Console/Commands/MakeModule.php @@ -42,15 +42,21 @@ class MakeModule extends Command $fields = $this->argument('fields'); $lower = strtolower($name); + // convert 'name:string,description:text' to 'name,description' + $fillable = explode(',', $fields); + $fillable = array_map(function($item) { + return explode(':', $item)[0]; + }, $fillable); + $fillable = join(',', $fillable); + $this->info("Creating module: {$name}"); - //$this->info("Fields: {$fields}"); Artisan::call('module:make', ['name' => [$name]]); Artisan::call('module:make-migration', ['name' => "create_{$lower}_table", '--fields' => $fields, 'module' => $name]); Artisan::call('module:migrate', ['module' => $name]); - Artisan::call('module:make-model', ['model' => $name, 'module' => $name]); + Artisan::call('module:make-model', ['model' => $name, 'module' => $name, '--fillable' => $fillable]); - Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'datatable']); + Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'datatable', '--fields' => $fields]); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'repository']); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'policy']); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'auth-provider']); @@ -70,4 +76,5 @@ class MakeModule extends Command ['fields', InputArgument::OPTIONAL, 'The fields of the module.'] ]; } + } diff --git a/app/Console/Commands/stubs/datatable.stub b/app/Console/Commands/stubs/datatable.stub index 77cf97357f18..89ee7602ffd1 100644 --- a/app/Console/Commands/stubs/datatable.stub +++ b/app/Console/Commands/stubs/datatable.stub @@ -15,6 +15,7 @@ class $CLASS$Datatable extends EntityDatatable public function columns() { return [ + $COLUMNS$ [ 'created_at', function ($model) {