Change table name using migration in Laravel

Usually when you need to change the table name you will change it manually. You can also change the table using migration.

Schema::rename($currentTableName, $newTableName);

If you want to remove an existing table add the below code to migration. If you have foreign keys indexes

and uniques you won’t be able to delete them after renaming.

Schema::table('new_table', function (Blueprint $table) {

$table->dropForeign(['transaction_id']);

});

In such case remove the foreign keys etc first then rename yourtable.

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');

});