Skip to content

Instantly share code, notes, and snippets.

@Frolki1-Dev
Last active September 2, 2020 20:35
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/b00ef1b855f1d43322d0fff9ca0daae4 to your computer and use it in GitHub Desktop.
Save Frolki1-Dev/b00ef1b855f1d43322d0fff9ca0daae4 to your computer and use it in GitHub Desktop.
Artisan command to create model, migrations, factories, controller and resources in one run
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class CreateEntities extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tool:create_entity {name : The name of the class}
{--m|minimal : Make only model and migration}
{--np|no_policy : No policy file needed}
{--nf|no_factory : No factory file needed}
{--nr|no_resources : No resource files needed}
{--s|seeder : Create a seeder file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new model, migration, factory, policy, resource, collection and controller in one command.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$class = $this->argument('name');
$model = sprintf('Models/%s', $class);
$controller = sprintf('Api/v1/%sController', $class);
$resource = sprintf('%s/%sResource', $class, $class);
$collection = sprintf('%s/%sCollection', $class, $class);
$policy = sprintf('%sPolicy', $class);
$seeder = sprintf('%sSeeder', $class);
// Run all command
if (!$this->option('minimal')) {
$model_arguments = ['--factory' => 1, '--migration' => 1, 'name' => $model];
if($this->option('no_factory')) {
unset($model_arguments['--factory']);
}
$this->call('make:model', $model_arguments);
if(!$this->option('no_policy')) {
$this->call('make:policy', ['-m' => $model, 'name' => $policy]);
}
$this->call('make:controller', ['--api' => 1, 'name' => $controller]);
if(!$this->option('no_resources')) {
$this->call('make:resource', ['name' => $resource]);
$this->call('make:resource', ['-c', 'name' => $collection]);
}
} else {
// Only minimal (model and factory)
$this->call('make:model', ['--migration' => 1, 'name' => $model]);
}
if($this->option('seeder')) {
$this->call('make:seeder', ['name' => $seeder]);
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment