Skip to content

Commit 471d1c9

Browse files
committed
add --force and --all option in seed command
1 parent 81b03b7 commit 471d1c9

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,25 @@ You can use ***--help*** option to have more informations about this command
4040
php artisan cmd:db:seed
4141
```
4242

43+
If you want to run all seed ***--all*** option
44+
45+
```bash
46+
php artisan cmd:db:seed --all
47+
```
48+
4349
If you want to run a specific class you can use ***--class*** option
4450

4551
```bash
4652
php artisan cmd:db:seed --class=UsersTableSeeder
4753
```
54+
55+
Run seed in production ***--force*** option
56+
57+
```bash
58+
php artisan cmd:db:seed --force
59+
```
60+
61+
4862
You can use ***--help*** option to have more informations about this command
4963

5064

src/Console/Commands/Database/SeedCommand.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class SeedCommand extends Command
1212
{
13-
const SEEDERS_NAMESPACE_PREFIX = 'Database\Seeders\\';
13+
const SEEDERS_NAMESPACE_PREFIX = 'Database\\Seeders\\';
1414

1515
/** @var Filesystem */
1616
protected $filesystem;
@@ -22,6 +22,8 @@ class SeedCommand extends Command
2222
*/
2323
protected $signature = 'cmd:db:seed
2424
{--c|class= : The seeder to run }
25+
{--all : Run all registered seed files }
26+
{--force : Force the operation to run when in production }
2527
';
2628

2729
/**
@@ -41,15 +43,33 @@ public function __construct()
4143

4244
public function handle()
4345
{
46+
if ($this->option('all')){
47+
$this->runSeed();
48+
exit;
49+
}
50+
4451
$seeders = Arr::wrap($this->option('class')) ?: $this->getSeedClass();
4552

4653
foreach ($seeders as $seeder) {
47-
$this->call('db:seed', [
48-
'--class' => $this->getSeederWithNamespace($seeder),
49-
]);
54+
$this->runSeed($seeder);
5055
}
5156
}
5257

58+
private function runSeed(?string $class = null) :void
59+
{
60+
$options = [];
61+
62+
if ($class){
63+
$options['--class'] = $this->getSeederWithNamespace($class);
64+
}
65+
66+
if ($force = $this->option('force')){
67+
$options['--force'] = $force;
68+
}
69+
70+
$this->call("db:seed", $options);
71+
}
72+
5373
private function getSeederWithNamespace(string $seeder) :string
5474
{
5575
if (Str::startsWith($seeder, self::SEEDERS_NAMESPACE_PREFIX)){

0 commit comments

Comments
 (0)