Skip to content

Instantly share code, notes, and snippets.

@Frolki1-Dev
Created April 18, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frolki1-Dev/bab84a5283c93620a476c2d0a69e22f8 to your computer and use it in GitHub Desktop.
Save Frolki1-Dev/bab84a5283c93620a476c2d0a69e22f8 to your computer and use it in GitHub Desktop.
[Laravel] A command which can run all factories in Laravel.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RunFactory extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'run:factory {num : The num of resources should be created.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run a factory';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$factories = [];
$choice = [];
$num = (int) $this->argument('num');
foreach (glob(database_path('factories/*.php')) as $file) {
array_push($factories, basename(explode('.', $file)[0]));
}
foreach (glob(app_path('models/*.php')) as $file) {
$f = basename(explode('.', $file)[0]);
if(array_search($f . 'Factory', $factories) !== false) {
array_push($choice, $f);
}
}
unset($factories);
$factory = $this->choice('Which factory should be run?', $choice);
factory("App\Models\\" . $factory, $num)->create();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment