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 (see README).
Verify Setup:
Run php artisan migrate:generate --help to confirm the package is registered and options are available.
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, indexes, and constraints.
Generating Migrations for Specific Tables:
php artisan migrate:generate --tables="users,posts,comments"
Excluding Tables:
php artisan migrate:generate --ignore="logs,failed_jobs"
Custom Output Path:
php artisan migrate:generate --path="database/migrations/custom"
custom/auth, custom/reports).Squashing Migrations:
php artisan migrate:generate --squash
Connection-Specific Migrations:
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.Custom Templates: Override default migration templates by specifying a custom path:
php artisan migrate:generate --template-path="resources/templates/migrations"
Foreign Key Handling:
--tables or generated first.--skip-foreign-keys to generate tables without constraints, then add them later:
php artisan migrate:generate --tables="users" --skip-foreign-keys
php artisan migrate:generate --tables="posts" --skip-foreign-keys
php artisan migrate:generate --tables="users,posts" --skip-foreign-keys=false
Collation and Encoding: Preserve database collation with:
php artisan migrate:generate --use-db-collation
Vendor Migrations:
Exclude vendor-specific tables (e.g., Laravel’s failed_jobs) with:
php artisan migrate:generate --skip-vendor
Views and Stored Procedures: Generate migrations for views or procedures:
php artisan migrate:generate --skip-views=false --skip-proc=false
Batch Logging: Assign migrations to a batch for ordering:
php artisan migrate:generate --log-with-batch=0
0 for initial setup).Circular Dependencies:
--skip-foreign-keys to generate tables first, then add constraints separately.Reserved Keywords:
order, group, or user may conflict with PHP/Laravel syntax.--default-index-names or --default-fk-names to generate Laravel-friendly names:
php artisan migrate:generate --default-index-names --default-fk-names
Large Databases:
--tables to limit scope or --squash to reduce file count.Connection Mismatches:
migrate:generate against a connection that doesn’t match config/database.php will fail silently.php artisan tinker
>>> \DB::connection()->getDatabaseName();
Schema Changes Mid-Generation:
--skip-log to avoid writing to the migrations table.PostgreSQL-Specific Issues:
jsonb, uuid) may not map cleanly to Laravel’s schema builder.$table->text('metadata')->comment('JSON data');
Dry Run:
Use --path="tmp/migrations" to test generation without overwriting production files.
Verbose Output: Enable debug mode to see SQL queries:
php artisan migrate:generate --tables="users" --verbose
Template Inspection: Check the generated template at:
vendor/kitloong/laravel-migrations-generator/src/Template/Blueprint.php
--template-path.Foreign Key Errors: If foreign keys fail to generate, manually inspect the database schema for:
int vs. bigint).UserID vs. user_id).SQLite Quirks:
Schema::connection('sqlite')->enableForeignKeyConstraints();
Custom Migration Logic: Extend the generator by publishing and modifying the service provider:
php artisan vendor:publish --provider="KitLoong\MigrationsGenerator\MigrationsGeneratorServiceProvider"
generate() in app/Providers/MigrationsGeneratorServiceProvider.php to add pre/post-processing.Event Listeners: Listen for migration generation events to modify output:
use KitLoong\MigrationsGenerator\Events\MigrationGenerated;
Event::listen(MigrationGenerated::class, function ($event) {
// Modify $event->migration->getContent()
});
Command Customization:
Create a custom command extending GenerateMigrationsCommand:
use KitLoong\MigrationsGenerator\Console\GenerateMigrationsCommand;
class CustomGenerateMigrationsCommand extends GenerateMigrationsCommand {
protected function getOptions() {
return array_merge(parent::getOptions(), [
['custom-option', 'c', InputOption::VALUE_OPTIONAL, 'Custom option description'],
]);
}
}
Schema Blueprint Extensions:
Add custom column types or modifiers by extending KitLoong\MigrationsGenerator\Template\Blueprint:
class CustomBlueprint extends Blueprint {
public function customColumn($name, $type) {
// Custom logic
}
}
Register it in your service provider’s boot() method.
Post-Generation Hooks:
Use Laravel’s migrated event to run logic after generation:
Event::listen('migrated', function ($migration) {
// Example: Optimize tables or run post-migration tasks
});
How can I help you explore Laravel packages today?