Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migration to change table name

I want to change my two table name in the Laravel, so do I have to manually change the table name or it can be possible through migration.

like image 528
HKumar Avatar asked Nov 24 '15 08:11

HKumar


People also ask

How do I change the table name in migration?

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: Schema::drop('users'); Schema::dropIfExists('users');

Can I rename migration in laravel?

Yes, you can name them however you want, but they will run in alphabetical order (which is why laravel timestamps them). Also you can change a migrations name after the fact (you've already run the migration).


2 Answers

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:

Schema::drop('users');  Schema::dropIfExists('users'); 

Just add that to a migration and it should work.

like image 83
GWed Avatar answered Sep 25 '22 02:09

GWed


You can rename table like that

Schema::rename('old_table', 'new_table'); 

BUT be careful if you have foreign keys, indexes and 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');  }); 
like image 32
Yevgeniy Afanasyev Avatar answered Sep 24 '22 02:09

Yevgeniy Afanasyev