MOVING COLUMN IN MIGRATION – LARAVEL

[norebro_social_networks icon_layout=”boxed”]

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 .

https://www.kodeinfo.com
Comments to: MOVING COLUMN IN MIGRATION – LARAVEL

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

    Attach images - Only PNG, JPG, JPEG and GIF are supported.

    Stay connected

    Categories

    Newsletter

    Tags

    Recent Comments

      Recent Posts

      Recent Comments

        Archives

        Categories

        Meta