Installation
composer require --dev xethron/migrations-generator
For Laravel <5.5, add Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider::class to config/app.php under providers.
Publish Config (Optional)
php artisan vendor:publish --provider="Xethron\MigrationsGenerator\MigrationsGeneratorServiceProvider"
Config file: config/migrations-generator.php.
First Use Case Generate migrations for an existing database:
php artisan db:generate:migrations
database/migrations/.YYYY_MM_DD_HHMMSS_create_tables.php.config/migrations-generator.php (customize table/column naming, excluded tables, etc.).db:generate:migrations (full schema generation).db:generate:migration (generate for a specific table).db:generate:seed (generate a seeder for existing data).database/migrations/ after running the command to see generated structure.Schema Analysis
php artisan db:generate:migration table_name to inspect a single table’s structure before full generation.--dry-run flag to preview SQL without writing files:
php artisan db:generate:migrations --dry-run
Customizing Output
table_name and column_name in config to match your project’s style (e.g., snake_case).config/migrations-generator.php under ignore_tables.generate_foreign_keys (default: true).Integration with Existing Migrations
up()/down() methods.Seeding Data
php artisan db:generate:seed --table=users
database/seeds/YYYY_MM_DD_HHMMSS_TableSeeder.php.CI/CD Pipeline
php artisan db:generate:migrations && php artisan migrate
Multi-Database Support
.env and use --connection=mysql flag:
php artisan db:generate:migration --connection=mysql users
Custom Templates
php artisan vendor:publish --tag=migrations-generator-templates
resources/views/vendor/migrations-generator/.Post-Generation Hooks
registerCommands in a service provider to extend functionality:
public function register()
{
$this->commands([
\Xethron\MigrationsGenerator\Console\GenerateMigrations::class,
]);
// Add custom logic here (e.g., auto-run migrations after generation)
}
Testing
php artisan migrate:fresh --env=testing
php artisan db:generate:migrations --env=testing
Foreign Key Conflicts
generate_foreign_keys = false) or manually resolve in migration files.Reserved Keywords
key, group, or order may cause SQL syntax errors.column_name config:
'column_name' => [
'key' => '`key`',
],
Large Databases
Data Type Mismatches
string instead of text).data_types mapping in config:
'data_types' => [
'tinyint(1)' => 'boolean',
'varchar(255)' => 'string',
],
Seeder Limitations
--chunk flag for large datasets:
php artisan db:generate:seed --table=users --chunk=100
Dry Runs
Use --dry-run to inspect SQL without file changes:
php artisan db:generate:migration users --dry-run
Verbose Output Enable debug mode for detailed logs:
php artisan db:generate:migrations --verbose
SQL Dump Check the raw SQL generated by the package:
// In a custom command or service provider:
$generator = new \Xethron\MigrationsGenerator\Generators\MigrationGenerator();
$sql = $generator->getMigrationSql('users');
Config Validation Validate config syntax before running:
php artisan config:clear
php artisan db:generate:migration users
Custom Generators Extend the base generator by creating a subclass:
namespace App\Generators;
use Xethron\MigrationsGenerator\Generators\MigrationGenerator as BaseGenerator;
class CustomMigrationGenerator extends BaseGenerator
{
protected function getColumnAttributes($column)
{
// Add custom logic (e.g., auto-add soft deletes)
$attributes = parent::getColumnAttributes($column);
$attributes['nullable'] = true;
return $attributes;
}
}
Register in AppServiceProvider@boot():
$this->app->bind(
\Xethron\MigrationsGenerator\Generators\MigrationGenerator::class,
App\Generators\CustomMigrationGenerator::class
);
Event Listeners Listen for migration generation events to modify output:
// In EventServiceProvider@boot():
event(new \Xethron\MigrationsGenerator\Events\MigrationGenerated(
$tableName,
$migrationFile
));
Database-Specific Tweaks Override the connection resolver for custom databases:
$this->app->bind(
\Illuminate\Database\Connection::class,
function () {
return \DB::connection('custom_db');
}
);
Testing Hooks Mock the generator in tests:
$generator = Mockery::mock(\Xethron\MigrationsGenerator\Generators\MigrationGenerator::class);
$generator->shouldReceive('generate')->andReturn($migration);
$this->app->instance(\Xethron\MigrationsGenerator\Generators\MigrationGenerator::class, $generator);
How can I help you explore Laravel packages today?