kitloong/laravel-migrations-generator
Generate Laravel migration files from an existing database, including columns, indexes, and foreign keys. Supports MariaDB, MySQL, PostgreSQL, SQL Server, and SQLite. Run artisan migrate:generate to scaffold migrations for all or selected tables.
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 foreign keys.
Key Files to Review:
config/database.php (ensure correct connection settings)database/migrations/ (verify structure and logic)Database Inspection:
php artisan migrate:generate to scan the database schema.Selective Generation:
php artisan migrate:generate --tables="users,posts"
php artisan migrate:generate --ignore="logs,temp_data"
Multi-Database Projects:
--connection for non-default databases:
php artisan migrate:generate --connection="secondary"
Schema::connection('secondary') calls.Custom Paths:
php artisan migrate:generate --path="database/migrations/feature_x"
Squashed Migrations:
php artisan migrate:generate --squash
Foreign Key Handling:
add_foreign_keys_to_users_table.php).--tables to avoid errors.Views and Stored Procedures:
php artisan migrate:generate --skip-views=false --skip-proc=false
--skip-views or --skip-proc.Date Control:
php artisan migrate:generate --date="2024-01-01 00:00:00"
+1 second to maintain order.CI/CD Pipelines:
--skip-log to avoid logging migrations in automated environments:
php artisan migrate:generate --skip-log
--with-has-table to safely generate migrations in shared environments:
php artisan migrate:generate --with-has-table
Team Collaboration:
--default-index-names and --default-fk-names to avoid DB-specific names:
php artisan migrate:generate --default-index-names --default-fk-names
--use-db-collation to preserve database collations:
php artisan migrate:generate --use-db-collation
Testing:
php artisan migrate:generate --connection="testing"
--path="tests/migrations" to isolate test migrations.Legacy Systems:
--squash and manually refactor complex migrations.Custom Templates:
php artisan migrate:generate --template-path="custom/templates"
Circular Dependencies:
--squash or manually order migrations.Reserved Keywords:
order, group, or user may conflict with PHP/Laravel.--default-index-names and manually rename columns in generated migrations.Connection Mismatches:
mysql instead of pgsql).--connection and verify config/database.php.Schema Differences:
bigIncrements vs. increments).--default-index-names for consistency.Views/Procedures:
--skip-views or --skip-proc if unsupported.Timestamp Collisions:
--date may cause duplicate timestamps.--log-with-batch to offset timestamps:
php artisan migrate:generate --log-with-batch=0
Vendor Migrations:
--skip-vendor to exclude them.Dry Runs:
php artisan migrate:generate --path="tmp/" # Check `tmp/` for output
Partial Generation:
php artisan migrate:generate --tables="users" --path="tmp/" --ignore="*" --fk-filename="debug_fks.php"
Log Inspection:
migrations table for existing entries:
SELECT * FROM migrations WHERE batch = 0;
--skip-log to avoid conflicts during testing.Template Overrides:
--template-path directory.dd() to a template to see generated column data.Database-Specific Quirks:
--use-db-collation for citext or custom collations.[] in generated migrations.Custom Column Types:
Column class (e.g., for jsonb or uuid).JsonColumn class in app/Extensions/JsonColumn.php.Pre/Post-Generation Hooks:
// In a service provider
event(new GeneratingMigrations);
event(new MigrationsGenerated);
app/Providers/EventServiceProvider.php.Dynamic Table Selection:
$tables = DB::connection('mysql')->select('SHOW TABLES');
$this->option('tables', implode(',', $tables));
Schema Modifiers:
Blueprint class to auto-add soft deletes or timestamps:
trait AutoTimestamps {
public function __construct() {
$this->timestamps();
}
}
Multi-Schema Support:
public, app in PostgreSQL):
php artisan migrate:generate --connection="pgsql" --tables="public.users,app.settings"
How can I help you explore Laravel packages today?