Skip to content

Commit

Permalink
Merge pull request #12479 from Gabrielcefetzada/feature/generate-mode…
Browse files Browse the repository at this point in the history
…l-and-factory-automatically-on-resource-command

Feature/generate model and factory automatically on resource command
  • Loading branch information
danharrin committed Apr 28, 2024
2 parents 5a7df73 + 00d5d6c commit 5bb040c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/panels/docs/03-resources/01-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ In this example, the model should exist at `Custom\Path\Models\Customer`. Please

Now when [generating the resource](#automatically-generating-forms-and-tables), Filament will be able to locate the model and read the database schema.

### Generating the model, migration and factory at the same name

If you'd like to save time when scaffolding your resources, Filament can also generate the model, migration and factory for the new resource at the same time using the `--model`, `--migration` and `--factory` flags in any combination:

```bash
php artisan make:filament-resource Customer --model --migration --factory
```

## Record titles

A `$recordTitleAttribute` may be set for your resource, which is the name of the column on your model that can be used to identify it from others.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class MakeResourceCommand extends Commands\MakeResourceCommand
{
protected $hidden = true;

protected $signature = 'filament:resource {name?} {--model-namespace=} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--panel=} {--F|force}';
protected $signature = 'filament:resource {name?} {--model-namespace=} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--panel=} {--model} {--migration} {--factory} {--F|force}';
}
29 changes: 27 additions & 2 deletions packages/panels/src/Commands/MakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MakeResourceCommand extends Command

protected $description = 'Create a new Filament resource class and default page classes';

protected $signature = 'make:filament-resource {name?} {--model-namespace=} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--panel=} {--F|force}';
protected $signature = 'make:filament-resource {name?} {--model-namespace=} {--soft-deletes} {--view} {--G|generate} {--S|simple} {--panel=} {--model} {--migration} {--factory} {--F|force}';

public function handle(): int
{
Expand All @@ -49,11 +49,36 @@ public function handle(): int
$model = 'Resource';
}

$modelNamespace = $this->option('model-namespace') ?? 'App\\Models';

if ($this->option('model')) {
$this->callSilently('make:model', [
'name' => "{$modelNamespace}\\{$model}",
]);
}

if ($this->option('migration')) {
$table = (string) str($model)
->classBasename()
->pluralStudly()
->snake();

$this->call('make:migration', [
'name' => "create_{$table}_table",
'--create' => $table,
]);
}

if ($this->option('factory')) {
$this->callSilently('make:factory', [
'name' => $model,
]);
}

$modelClass = (string) str($model)->afterLast('\\');
$modelSubNamespace = str($model)->contains('\\') ?
(string) str($model)->beforeLast('\\') :
'';
$modelNamespace = $this->option('model-namespace') ?? 'App\\Models';
$pluralModelClass = (string) str($modelClass)->pluralStudly();
$needsAlias = $modelClass === 'Record';

Expand Down

0 comments on commit 5bb040c

Please sign in to comment.