Skip to content

Commit

Permalink
laravel 8 factories
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdertCO committed Sep 8, 2020
1 parent 690d84d commit 660720d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Console/Run/RunFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class RunFactory extends Command
protected $description = 'Run a factory';

protected $signature = 'run:factory
{model= : The model to run the factory for}
{amount= : The amount of records generated by the factory}';
{model? : The model to run the factory for}
{amount? : The amount of records generated by the factory}';

/**
* Execute the console command.
Expand All @@ -42,7 +42,11 @@ public function handle()
$amount = $this->getAmountOption();

try {
factory($model, $amount)->create();
$model::factory()->count($amount)->create();
}
catch (\BadMethodCallException $e) {
$this->error('Did you include the `HasAttributes` trait on the model?');
return;
}
catch (\InvalidArgumentException $e) {
$this->error('Factory does not exist, ' . $e->getMessage());
Expand All @@ -61,8 +65,8 @@ protected function getModelOption()
$model = $this->argument('model');
}

if (!Str::startsWith($model, 'App\\')) {
$model = 'App\\' . $model;
if (!Str::startsWith($model, 'App\\Models\\')) {
$model = 'App\\Models\\' . $model;
}

if (!\class_exists($model)) {
Expand Down

0 comments on commit 660720d

Please sign in to comment.