1

We recently made a switch of service provider such that multiple columns in a DB table for this project will need to be renamed.

I am aware of this post which shows how to rename 1 column from 1 table:

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

Is there a way to execute this same migration with multiple columns ? (1 migration, not more than 1...trying to minimize number of migration files created)

aynber
  • 22,380
  • 8
  • 50
  • 63

1 Answers1

2

You can just add multiple renameColumn(); statements for each column that needs to be updated in that given table. Just need to come up with a whatever name you guys/gals use for your migration files.

Just a sample of what I ran

class MultipleColumnUpdate extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function ($table) {
            $table->renameColumn('name', 'user_name');
            $table->renameColumn('email', 'work_email');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function ($table) {
            $table->renameColumn('user_name', 'name');
            $table->renameColumn('work_email', 'email');
        });
    }
}
Andrew Nolan
  • 1,987
  • 2
  • 20
  • 23
  • Somehow, I'm not being able to do this on Laravel 5.3, it throws "there is no column 'old_name'"... – giovannipds Sep 01 '16 at 12:09
  • 1
    @giovannipds if you want to share your migration, I can take a look. I haven't used 5.3 yet or reviewed the change documentation. – Andrew Nolan Sep 02 '16 at 18:55
  • Thanks @Andrew Nolan, but I got it sometime later. It was probably just an error in my database, of course it'd throw that message. =) 5.3 is almost the same of 5.2. – giovannipds Oct 27 '16 at 08:52
  • 1
    In case someone finds this in the future. SQLite seems to have problems with this. You need to use two separate Schema::table() calls, one for each column rename. Same goes for dropColumn statements. – Mr_Chimp Aug 23 '18 at 16:10