How can I rename column in laravel using migration?

前端 未结 6 634
名媛妹妹
名媛妹妹 2020-12-04 20:55

I have columns as mentioned bellow:

public function up()
{
    Schema::create(\'stnk\', function(Blueprint $table)
    {
        $table->increments(\'id\         


        
相关标签:
6条回答
  • 2020-12-04 21:03

    first thing you want to do is to create your migration file.

    Type in your command line

    php artisan make:migration rename_stk_column --table="YOUR TABLE" --create
    

    After creating the file. Open the new created migration file in your app folder under database/migrations.

    In your up method insert this:

    Schema::table('stnk', function(Blueprint $table)
        {
            $table->renameColumn('id', 'id_stnk');
        });
    }
    

    and in your down method:

        Schema::table('stnk', function(Blueprint $table)
        {
            $table->renameColumn('id_stnk', 'id);
        });
    }
    

    then in your command line just type

    php artisan migrate
    

    Then wollah! you have just renamed id to id_stnk. BTW you can use

    php artisan migrate:rollback
    

    to undo the changes. Goodluck

    0 讨论(0)
  • 2020-12-04 21:06

    The above answer is great or if it will not hurt you, just rollback the migration and change the name and run migration again.

     php artisan migrate:rollback
    
    0 讨论(0)
  • 2020-12-04 21:11

    You need to create another migration file - and place it in there:

    Run

    Laravel 4:    php artisan migrate:make rename_stnk_column
    Laravel 5:    php artisan make:migration rename_stnk_column
    

    Then inside the new migration file place:

    class RenameStnkColumn extends Migration
    {
    
        public function up()
        {
            Schema::table('stnk', function(Blueprint $table) {
                $table->renameColumn('id', 'id_stnk');
            });
        }
    
    
        public function down()
        {
            Schema::table('stnk', function(Blueprint $table) {
                $table->renameColumn('id_stnk', 'id');
            });
        }
    
    }
    
    0 讨论(0)
  • 2020-12-04 21:13

    Throwing my $0.02 in here since none of the answers worked, but did send me on the right path. What happened was that a previous foreign constraint was throwing the error. Obvious when you think about it.

    So in your new migration's up method, first drop that original constraint, rename the column, then add the constraint again with the new column name. In the down method, you do the exact opposite so that it's back to the sold setting.

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('proxy4s', function (Blueprint $table) {
            // Drop it
            $table->dropForeign(['server_id']);
    
            // Rename
            $table->renameColumn('server_id', 'linux_server_id');
    
            // Add it
            $table->foreign('linux_server_id')->references('id')->on('linux_servers');
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('proxy4s', function (Blueprint $table) {
            // Drop it
            $table->dropForeign(['linux_server_id']);
    
            // Rename
            $table->renameColumn('linux_server_id', 'server_id');
    
            // Add it
            $table->foreign('server_id')->references('id')->on('linux_servers');
        });
    }
    

    Hope this saves someone some time in the future!

    0 讨论(0)
  • 2020-12-04 21:22

    Follow these steps, respectively for rename column migration file.

    1- Is there Doctrine/dbal library in your project. If you don't have run the command first

    composer require doctrine/dbal
    

    2- create update migration file for update old migration file. Warning (need to have the same name)

    php artisan make:migration update_oldFileName_table
    

    for example my old migration file name: create_users_table update file name should : update_users_table

    3- update_oldNameFile_table.php

    Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
    });
    

    'from' my old column name and 'to' my new column name

    4- Finally run the migrate command

    php artisan migrate
    

    Source link: laravel document

    0 讨论(0)
  • 2020-12-04 21:27

    Renaming Columns (Laravel 5.x)

    To rename a column, you may use the renameColumn method on the Schema builder. *Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file.*

    Or you can simply required the package using composer...

    composer require doctrine/dbal
    

    Source: https://laravel.com/docs/5.0/schema#renaming-columns

    Note: Use make:migration and not migrate:make for Laravel 5.x

    0 讨论(0)
提交回复
热议问题