Laravel Migration to change table name

前端 未结 4 888
挽巷
挽巷 2020-12-08 05:57

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.

相关标签:
4条回答
  • 2020-12-08 06:39

    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');
     });
    
    0 讨论(0)
  • 2020-12-08 06:44

    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
    
    0 讨论(0)
  • 2020-12-08 06:45

    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.

    0 讨论(0)
  • 2020-12-08 06:49

    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:

    Schema::drop('users');
    
    Schema::dropIfExists('users');
    
    0 讨论(0)
提交回复
热议问题