Scroll to top

MOVING COLUMN IN MIGRATION – LARAVEL

It is a common scenario when you want to add extra columns to your table but at an appropriate location like if you forgot to add last name and add it later using migration column will be appended at the last .

Columns

  • id
  • first_name
  • mobile_no
  • updated_at
  • created_at
  • last_name

Whenever I browse database using phpmyadmin i expect to see last_name after first_name. last_name column at the end is a bit of confusion and frustration to how the structure is organized. There is no straight way to do this using laravel standard so we have to execute the raw query. we can solve this problem in the following way.

public function up()
{
 
DB::statement("ALTER TABLE profile MODIFY COLUMN last_name VARCHAR(255) AFTER first_name");
 
}
 
public function down()
{
 
DB::statement("ALTER TABLE profile MODIFY COLUMN last_name VARCHAR(255) AFTER created_at");
 
}

We hope it helped you .

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 *