Skip to content

Instantly share code, notes, and snippets.

@akutaktau
Last active September 13, 2024 07:17
Show Gist options
  • Save akutaktau/185e60dbb36bbdf4b2fc66b58c65aba8 to your computer and use it in GitHub Desktop.
Save akutaktau/185e60dbb36bbdf4b2fc66b58c65aba8 to your computer and use it in GitHub Desktop.
Generate all models based on table in database for Laravel 11
<?php
// app/Console/Commands/Generate/GenerateModels.php
namespace App\Console\Commands\Generate;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
class GenerateModels extends Command
{
protected $signature = 'generate:models';
protected $description = 'Generate models for all tables in the database';
public function __construct()
{
parent::__construct();
}
public function handle()
{
//show all tables in database
$tables = DB::select('SHOW TABLES');
$tableKey = 'Tables_in_' . env('DB_DATABASE');
foreach ($tables as $table) {
$tableName = $table->$tableKey;
//camelcase and singularize tablename
$modelName = ucfirst(Str::camel(Str::singular($tableName)));
$this->info("Creating model for table: $tableName");
//trigger make:model command
$this->call('make:model', ['name' => $modelName]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment