public function handle()
{
$name = $this->argument('name');
$modelName = str_singular($name);
$migrationName = str_plural(snake_case($name));
$tableName = $migrationName;
$routeGroup = $this->option('route-group');
$this->routeName = $routeGroup ? $routeGroup . '/' . snake_case($name, '-') : snake_case($name, '-');
$perPage = intval($this->option('pagination'));
$controllerNamespace = $this->option('controller-namespace') ? $this->option('controller-namespace') . '\\' : '';
$modelNamespace = $this->option('model-namespace') ? trim($this->option('model-namespace')) . '\\' : '';
$fields = rtrim($this->option('fields'), ';');
$primaryKey = $this->option('pk');
$viewPath = $this->option('view-path');
$foreignKeys = $this->option('foreign-keys');
$fieldsArray = explode(';', $fields);
$fillableArray = [];
foreach ($fieldsArray as $item) {
$spareParts = explode('#', trim($item));
$fillableArray[] = $spareParts[0];
}
$commaSeparetedString = implode("', '", $fillableArray);
$fillable = "['" . $commaSeparetedString . "']";
$localize = $this->option('localize');
$locales = $this->option('locales');
$indexes = $this->option('indexes');
$relationships = $this->option('relationships');
$validations = trim($this->option('validations'));
$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: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]);
}
// For optimizing the class loader
$this->callSilent('optimize');
// Updating the Http/routes.php file
$routeFile = app_path('Http/routes.php');
if (\App::VERSION() >= '5.3') {
$routeFile = base_path('routes/web.php');
}
if (file_exists($routeFile) && strtolower($this->option('route')) === 'yes') {
$this->controller = $controllerNamespace != '' ? $controllerNamespace . '\\' . $name . 'Controller' : $name . 'Controller';
$isAdded = File::append($routeFile, "\n" . implode("\n", $this->addRoutes()));
if ($isAdded) {
$this->info('Crud/Resource route added to ' . $routeFile);
} else {
$this->info('Unable to add the route to ' . $routeFile);
}
}
}