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 .
No Comments
Leave a comment Cancel