lk.zachem.info/app/Console/Commands/CreateModelAndMigrationCommand.php
2025-04-09 09:47:12 +08:00

101 lines
2.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class CreateModelAndMigrationCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:create {name}
{--momi=}
{--create_model=}
{--create_migration=}
{--add_migration=}
';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
if ($this->option('momi')) {
$this->input->setOption('create_model', $this->option('momi'));
$this->input->setOption('create_migration', $this->option('momi'));
}
if ($this->option('create_model')) {
$this->createModel();
}
if ($this->option('create_migration')) {
$this->createMigration();
}
if ($this->option('add_migration')) {
$this->addMigration();
}
}
private function createModel()
{
try
{
$path = trim($this->argument('name'));
$model = Str::singular(class_basename($this->option('create_model')));
$this->call('make:model', [
'name' => 'App\\Modules\\' . $path . '\\Models\\' . $model
]);
}
catch (\Exception $e)
{
$e->getMessage();
}
}
private function createMigration()
{
$path = trim($this->argument('name'));
$table = Str::plural(Str::snake(class_basename($this->option('create_migration'))));
try
{
$this->call('make:migration', [
'name' => 'create_' . $table . '_table',
'--create' => $table,
'--path' => 'app/Modules/' . $path . '/Database/Migrations'
]);
}
catch (\Exception $e)
{
$e->getMessage();
}
}
private function addMigration()
{
$path = trim($this->argument('name'));
$table = $this->option('add_migration');
try
{
$this->call('make:migration', [
'name' => $table,
'--path' => 'app/Modules/' . $path . '/Database/Migrations'
]);
}
catch (\Exception $e)
{
$e->getMessage();
}
}
}