Laravel Voyager创建表时未生成迁移文件的解决方案求助
Solutions for Generating Migration Files After Creating Tables with Voyager's Database Manager
Got it, I've run into this exact issue with Voyager's Database Manager before—super frustrating when you need to keep your team's DB schemas in sync via Git. Here are a few solid solutions to fix this:
1. Generate Migrations from Existing Tables (Quick Retroactive Fix)
If you've already created tables via Voyager and need to catch up on migration files, use a dedicated Laravel package to scan your database and build migrations automatically:
- Install the migration generator package via Composer:
composer require kitloong/laravel-migrations-generator --dev - Run the command to generate migrations for specific tables (replace
your_table_namewith your actual table name; omit the table name to generate migrations for all tables):php artisan migrate:generate your_table_name - This will create fully structured migration files in
database/migrationsthat match your Voyager-created table schemas. Commit these files to Git, and your team can runphp artisan migrateto sync their local databases instantly.
2. Modify Voyager to Auto-Generate Migrations (Permanent Fix)
If you want Voyager's Database Manager to create migrations automatically every time you add or modify tables, you can extend its core logic (avoid editing vendor files directly—create an override instead):
- Create a custom controller that extends Voyager's
DatabaseController, then override theaddTablemethod - After the table is created successfully, add code to generate a migration file programmatically using Laravel's
MigrationCreatorclass:use Illuminate\Database\Migrations\MigrationCreator; use Illuminate\Support\Facades\App; // Insert this after the table creation logic $creator = App::make(MigrationCreator::class); $migrationName = 'create_' . $request->input('name') . '_table'; $creator->create($migrationName, database_path('migrations')); // You'll also need to populate the migration file with column definitions // Extract the column data from the request used to create the table - Update your routes to point to this custom controller instead of the default Voyager one. Now every table created via Voyager will have a matching migration file.
3. Shift to a Migration-First Workflow (Long-Term Best Practice)
For smoother team collaboration, align with Laravel's native workflow:
- First write a migration file using
php artisan make:migration create_your_table_name_table - Define your table schema in the migration's
upmethod, then runphp artisan migrateto create the table - Use Voyager's UI only for managing data, editing field details, or setting up BREAD (Browse, Read, Edit, Add, Delete) functionality
- This ensures migrations are always the single source of truth for your database schema, making Git-based syncing straightforward for your entire team
内容的提问来源于stack exchange,提问作者Lohith
相关产品推荐
相关产品推荐

