How can I rename column in laravel using migration?

前端 未结 6 636
名媛妹妹
名媛妹妹 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

提交回复
热议问题