public function handle()
{
$this->crudName = strtolower($this->argument('name'));
$this->crudNameCap = ucwords($this->crudName);
$this->crudNameSingular = str_singular($this->crudName);
$this->modelName = str_singular($this->argument('name'));
$this->primaryKey = $this->option('pk');
$this->routeGroup = $this->option('route-group') ? $this->option('route-group') . '/' : $this->option('route-group');
$this->viewName = snake_case($this->argument('name'), '-');
$viewDirectory = config('view.paths')[0] . '/';
if ($this->option('view-path')) {
$this->userViewPath = $this->option('view-path');
$path = $viewDirectory . $this->userViewPath . '/' . $this->viewName . '/';
} else {
$path = $viewDirectory . $this->viewName . '/';
}
if (!File::isDirectory($path)) {
File::makeDirectory($path, 0755, true);
}
$fields = $this->option('fields');
$fieldsArray = explode(';', $fields);
$this->formFields = [];
$validations = $this->option('validations');
if ($fields) {
$x = 0;
foreach ($fieldsArray as $item) {
$itemArray = explode('#', $item);
$this->formFields[$x]['name'] = trim($itemArray[0]);
$this->formFields[$x]['type'] = trim($itemArray[1]);
$this->formFields[$x]['required'] = preg_match('/' . $itemArray[0] . '/', $validations) ? true : false;
if ($this->formFields[$x]['type'] == 'select' && isset($itemArray[2])) {
$options = trim($itemArray[2]);
$options = str_replace('options=', '', $options);
$optionsArray = explode(',', $options);
$commaSeparetedString = implode("', '", $optionsArray);
$options = "['" . $commaSeparetedString . "']";
$this->formFields[$x]['options'] = $options;
}
$x++;
}
}
foreach ($this->formFields as $item) {
$this->formFieldsHtml .= $this->createField($item);
}
$i = 0;
foreach ($this->formFields as $key => $value) {
if ($i == $this->defaultColumnsToShow) {
break;
}
$field = $value['name'];
$label = ucwords(str_replace('_', ' ', $field));
if ($this->option('localize') == 'yes') {
$label = '{{ trans(\'' . $this->crudName . '.' . $field . '\') }}';
}
$this->formHeadingHtml .= '<th> ' . $label . ' </th>';
$this->formBodyHtml .= '<td>{{ $item->' . $field . ' }}</td>';
$this->formBodyHtmlForShowView .= '<tr><th> ' . $label . ' </th><td> {{ $%%crudNameSingular%%->' . $field . ' }} </td></tr>';
$i++;
}
// For index.blade.php file
$indexFile = $this->viewDirectoryPath . 'index.blade.stub';
$newIndexFile = $path . 'index.blade.php';
if (!File::copy($indexFile, $newIndexFile)) {
echo "failed to copy {$indexFile}...\n";
} else {
$this->templateIndexVars($newIndexFile);
}
// For form.blade.php file
$formFile = $this->viewDirectoryPath . 'form.blade.stub';
$newFormFile = $path . 'form.blade.php';
if (!File::copy($formFile, $newFormFile)) {
echo "failed to copy {$formFile}...\n";
} else {
$this->templateFormVars($newFormFile);
}
// For create.blade.php file
$createFile = $this->viewDirectoryPath . 'create.blade.stub';
$newCreateFile = $path . 'create.blade.php';
if (!File::copy($createFile, $newCreateFile)) {
echo "failed to copy {$createFile}...\n";
} else {
$this->templateCreateVars($newCreateFile);
}
// For edit.blade.php file
$editFile = $this->viewDirectoryPath . 'edit.blade.stub';
$newEditFile = $path . 'edit.blade.php';
if (!File::copy($editFile, $newEditFile)) {
echo "failed to copy {$editFile}...\n";
} else {
$this->templateEditVars($newEditFile);
}
// For show.blade.php file
$showFile = $this->viewDirectoryPath . 'show.blade.stub';
$newShowFile = $path . 'show.blade.php';
if (!File::copy($showFile, $newShowFile)) {
echo "failed to copy {$showFile}...\n";
} else {
$this->templateShowVars($newShowFile);
}
$this->info('View created successfully.');
}