kitloong/laravel-migrations-generator
Generate Laravel migration files from an existing database schema, including columns, indexes, and foreign keys. Works with MariaDB/MySQL, PostgreSQL, SQL Server, and SQLite. Generate all tables or target/ignore specific tables via Artisan.
Installation:
composer require --dev kitloong/laravel-migrations-generator
Laravel auto-registers the service provider; Lumen requires manual registration in bootstrap/app.php.
First Use Case: Generate migrations for all tables in your default database:
php artisan migrate:generate
This creates individual migration files for each table in database/migrations/, including columns, indexes, and foreign keys.
Where to Look First:
php artisan help migrate:generate for CLI options.database/migrations/[datetime]_create_[table]_table.php.Generate All Tables:
php artisan migrate:generate
2024_01_01_000000_create_users_table.php).Selective Generation:
php artisan migrate:generate --tables="users,posts" --ignore="temp_tables"
Squashed Migrations:
php artisan migrate:generate --squash
2024_01_01_000000_create_all_tables.php).Custom Paths:
php artisan migrate:generate --path="database/migrations/custom"
Foreign Key Handling:
add_foreign_keys_to_users_table.php).--skip-foreign-keys to generate tables first, then manually add constraints later.Multi-Database Projects:
php artisan migrate:generate --connection="secondary"
Date Control:
php artisan migrate:generate --date="2024-01-01 00:00:00"
Schema Checks:
php artisan migrate:generate --with-has-table
Schema::hasTable() checks to migrations, preventing errors in environments where tables may already exist.Template Customization:
--template-path="path/to/custom/templates".DATETIME to timestamp instead of dateTime).CI/CD Pipelines:
--skip-log to avoid logging migrations to the migrations table (useful for generated schemas).--squash for atomic migration files:
php artisan migrate:generate --squash --skip-log
Foreign Key Dependencies:
--tables="parent,child" or generate all tables first, then filter.Reserved Keywords:
order, group, or user may conflict with PHP/Laravel keywords.--default-index-names or --default-fk-names to avoid DB-specific names in migrations.Collation Mismatches:
utf8mb4_unicode_ci).--use-db-collation to include collation in migrations.Views and Stored Procedures:
--skip-views --skip-proc) and generate separately if needed.Vendor Migrations:
--skip-vendor or exclude vendor tables (e.g., --ignore="migration_locks").Dry Run:
php artisan migrate:generate --path="/tmp" # Output to temp dir
/tmp/[datetime]_*.php for errors.Connection Issues:
config/database.php matches the --connection flag.php artisan tinker
>>> \DB::connection('secondary')->select('SHOW TABLES');
Template Errors:
getTableBlueprint().php artisan migrate:generate -vvv
Custom Column Types:
app/Providers/MigrationsGeneratorServiceProvider.php:
$generator->columnTypes = [
'DATETIME' => 'timestamp',
'TINYINT(1)' => 'boolean',
];
Post-Generation Hooks:
registerMigrationsGenerator in AppServiceProvider to modify migrations after generation:
$generator->afterGenerate(function ($migration) {
$migration->prepend("// Custom header\n");
});
Batch Processing:
--log-with-batch=0 to control migration order:
php artisan migrate:generate --log-with-batch=0 --tables="batch1_tables"
php artisan migrate:generate --log-with-batch=1 --tables="batch2_tables"
Environment-Specific Config:
.env variables to dynamically set options:
php artisan migrate:generate --date="$(date +'%Y-%m-%d %H:%M:%S')"
config/migrations-generator.php (if extended).How can I help you explore Laravel packages today?