Skip to content

Commit 7e8ef30

Browse files
committed
Merge branch 'Wiz-Amit-master' into develop
2 parents 5c5c32c + 8a684f5 commit 7e8ef30

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/Commands/MakeCommand.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\File;
77
use Illuminate\Support\Str;
88
use Livewire\Commands\ComponentParser;
9+
use Illuminate\Database\Eloquent\Model;
910
use Livewire\Commands\MakeCommand as LivewireMakeCommand;
1011

1112
/**
@@ -141,8 +142,8 @@ public function classContents(): string
141142
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table-with-model.stub');
142143

143144
$contents = str_replace(
144-
['[namespace]', '[class]', '[model]', '[model_import]'],
145-
[$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport()],
145+
['[namespace]', '[class]', '[model]', '[model_import]', '[columns]'],
146+
[$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport(), $this->generateColumns($this->getModelImport())],
146147
$template
147148
);
148149
} else {
@@ -204,4 +205,40 @@ public function getModelImport(): string
204205

205206
return 'App\Models\\' . $this->model;
206207
}
208+
209+
/**
210+
* @param string $modelName
211+
* @return string
212+
* @throws Exception
213+
*/
214+
private function generateColumns(string $modelName): string
215+
{
216+
$model = new $modelName();
217+
218+
if ($model instanceof Model === false) {
219+
throw new \Exception('Invalid model given.');
220+
}
221+
222+
$getFillable = array_merge(
223+
[$model->getKeyName()],
224+
$model->getFillable(),
225+
['created_at', 'updated_at']
226+
);
227+
228+
$columns = "[\n";
229+
230+
foreach ($getFillable as $field) {
231+
if (in_array($field, $model->getHidden())) {
232+
continue;
233+
}
234+
235+
$title = Str::of($field)->replace('_', ' ')->ucfirst();
236+
237+
$columns .= ' Column::make("' . $title . '", "' . $field . '")' . "\n" . ' ->sortable(),' . "\n";
238+
}
239+
240+
$columns .= " ]";
241+
242+
return $columns;
243+
}
207244
}

src/Commands/table-with-model.stub

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ class [class] extends DataTableComponent
1212

1313
public function columns(): array
1414
{
15-
return [
16-
Column::make('Column Name'),
17-
];
15+
return [columns];
1816
}
1917

2018
public function query(): Builder

0 commit comments

Comments
 (0)