我想在 Laravel 中更改我的两个表名,所以我必须手动更改表名,或者可以通过迁移实现。
from the docs laravel.com/docs/5.1/migrations#renaming-and-dropping-tables
To change a table name, you can do this:
Schema::rename($currentTableName, $newTableName);
You can use the drop or dropIfExists methods to remove an existing table:
drop
dropIfExists
Schema::drop('users'); Schema::dropIfExists('users');
Just add that to a migration and it should work.
To rename an existing database table, use the rename method:
Schema::rename($from, $to);
To drop an existing table, you may use the drop or dropIfExists methods:
You can rename table like that
Schema::rename('old_table', 'new_table');
BUT be careful if you have foreign keys, indexes and unique-s.
foreign keys
indexes
unique-s
you will not be able to deleted them after renaming, like thiat
Schema::table('new_table', function (Blueprint $table) { $table->dropForeign(['transaction_id']); });
because they will have old names and these names have table name in them.
Thus, I recommend deleting foreign keys and other stuff first
Schema::table('old_table', function (Blueprint $table) { $table->dropForeign(['transaction_id']); }); Schema::rename('old_table', 'new_table'); Schema::table('new_table', function (Blueprint $table) { $table->foreign('transaction_id')->references('id')->on('transactions'); });
Firstly, use CLI command to create a migration:
php artisan make:migration rename_table
Now, in the up method of the new migration class, use the rename method to change table name:
Schema::rename('old_table_name', 'new_table_name');
Next, execute the migration command:
php artisan migrate
Firstly, run this in your terminal to create a migration file to rename a table:
php artisan make:migration rename_old_name_to_new_name_table
Then in the up method, you should have this:
public function up() { Schema::rename('old_table_name', 'new_table_name'); }
Then in the down method, you should have this in case you want to revert previous changes made:
public function down() { Schema::rename('new_table_name', 'old_table_name'); }