Skip to content

Commit 407d67d

Browse files
committed
wip
1 parent 07ba12a commit 407d67d

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

src/Commands/MakeCommand.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
78
use Livewire\Commands\ComponentParser;
89
use Livewire\Commands\MakeCommand as LivewireMakeCommand;
910

1011
class MakeCommand extends Command
1112
{
1213
protected $parser;
14+
protected $model = null;
1315
/**
1416
* The name and signature of the console command.
1517
*
1618
* @var string
1719
*/
18-
protected $signature = 'make:table {name} {--force}';
20+
protected $signature = 'make:table
21+
{name : The name of your Livewire class}
22+
{model? : The name of the model you want to use in this table }
23+
{--force}';
1924

2025
/**
2126
* The console command description.
@@ -40,6 +45,7 @@ public function handle()
4045
return;
4146
}
4247

48+
$this->model = Str::studly($this->argument('model'));
4349
$force = $this->option('force');
4450

4551
$this->createClass($force);
@@ -74,12 +80,34 @@ protected function ensureDirectoryExists($path)
7480

7581
public function classContents()
7682
{
77-
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table.stub');
83+
if($this->model) {
84+
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table-with-model.stub');
85+
86+
return preg_replace_array(
87+
['/\[namespace\]/', '/\[class\]/', '/\[model\]/', '/\[model_import\]/'],
88+
[$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport()],
89+
$template
90+
);
91+
} else {
92+
$template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table.stub');
93+
94+
return preg_replace_array(
95+
['/\[namespace\]/', '/\[class\]/'],
96+
[$this->parser->classNamespace(), $this->parser->className()],
97+
$template
98+
);
99+
}
100+
}
78101

79-
return preg_replace_array(
80-
['/\[namespace\]/', '/\[class\]/'],
81-
[$this->parser->classNamespace(), $this->parser->className()],
82-
$template
83-
);
102+
public function getModelImport()
103+
{
104+
if(File::exists(app_path('Models/' . $this->model . '.php'))) {
105+
return 'App\Models\\' . $this->model;
106+
}
107+
if(File::exists(app_path($this->model . '.php'))) {
108+
return 'App\\' . $this->model;
109+
}
110+
$this->error('Could not find path to model');
111+
return 'App\Models\\' . $this->model;
84112
}
85113
}

src/Commands/table-with-model.stub

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace [namespace];
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Views\Column;
8+
use [model_import];
9+
10+
class [class] extends DataTableComponent
11+
{
12+
13+
public function columns(): array
14+
{
15+
return [
16+
Column::make('Column Name'),
17+
];
18+
}
19+
20+
public function query(): Builder
21+
{
22+
return [model]::query();
23+
}
24+
}

0 commit comments

Comments
 (0)