Scroll to top

RENAMING COLUMNS IN MIGRATION

If you are using Laravel then code the laravel way or go back to plain php .

Scenario

You have created a column with type Integer and looking around to change it to String. You may think of the below solutions

1. Running raw query using DB::statement or DB::select.

2. Drop the column and create a new one with type String.

I am sure you will go with second solutions if you are a true laravel lover. If you are trying to do something like below in your migration file think again.

Schema::table('content', function(Blueprint $table)
 {
      $table->dropColumn("description");
      $table->string("description",50);
 
 });

It will give you error Column already exist because you are doing drop and add on the same instance before committing the drop statement

Schema will commit the changes you did to the table after the last statement so something like below will work.

Schema::table('content', function(Blueprint $table)
{
            $table->dropColumn("description");
 
});
 
Schema::table('content', function(Blueprint $table)
{
            $table->string("description",50);
});

Thanks

KodeInfo

Author avatar

admin

http://www.kodeinfo.com
Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *