Created
April 18, 2020 14:51
-
-
Save Frolki1-Dev/bab84a5283c93620a476c2d0a69e22f8 to your computer and use it in GitHub Desktop.
[Laravel] A command which can run all factories in Laravel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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