6

How do I re-arrange a column in a Laravel migration file for a MySQL database?

So far I have the following:

$table->date('foo')->after('bar')->change();

However, it does not seem to re-arrange the column.

Why not and how can I fix this?

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
  • btw: what you need to rearrange the column order? it makes no difference technically – Marcin Orlowski Mar 03 '17 at 10:50
  • If you consider using `DB::raw` you can do something like `DB::raw('ALTER TABLE tablename CHANGE COLUMN foo foo date AFTER bar;')` – KuKeC Mar 03 '17 at 10:51
  • 1
    @MarcinOrlowski The main issue for me is just organisation. For example, when I'm looking through the database in PHPMyAdmin, I want to see this specific column next to another as they are related. Else there may be a LOT of horizontal scrolling. – Yahya Uddin Mar 03 '17 at 10:53
  • And i think you should remove `change();` and only leave `$table->date('foo')->after('bar');`. Because method `change();` allows you to modify an existing column to a new type, or modify the column's attributes, not order of column. – KuKeC Mar 03 '17 at 10:58
  • 1
    @KuKec If you do that it will attempt to create a new column with the same name, giving the error: `Column already exists: 1060 Duplicate column name 'example' (SQL: alter table \`example\` add \`foo\` date not null after \`bar\`)` – Yahya Uddin Mar 03 '17 at 11:12
  • Could you please try with first removing same column that you want to re-arrange and then create again with "after" modifier in column. If you have major database then for your safety please take backup then do. –  Mar 03 '17 at 11:46
  • That of course works. But that would lose my data. – Yahya Uddin Mar 03 '17 at 11:47
  • Yes that would lose your data for that i mentioned like first export it and once done then again import your data again hope that would help you :) –  Mar 03 '17 at 11:50
  • Posible duplicate of https://stackoverflow.com/q/20340778/10539212 – Phantom1412 May 26 '21 at 04:10

2 Answers2

6

Can't see anything in Laravel Schema API that will allow you to rearrange columns. Your best bet will be to use raw SQL statement as below.

 DB::statement("ALTER TABLE table_name MODIFY COLUMN col_name col_definition AFTER another_col");
Silvanus Matiku
  • 149
  • 1
  • 6
0

First Run following artisan command

php artisan make:migration reorganize_order_of_column_<col_name> --table=<table_name>

where col_name is name of column which you want to re-order and table_name if the name of your table

and then it will generate on new file in migration folder then update that file as following function

public function up()
    {
        DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <col_name> <col_description> AFTER <second_col_name>");

    }

    public function down()
    {
        DB::statement("ALTER TABLE <table_name> MODIFY COLUMN <col_name> <col_description> AFTER <second_col_name>");
    }
  • table_name is the name of table which you want to modify
  • col_name is the name of column which you want to re-order
  • col_description is the type and length of column like VARCHAR(5),DATE
  • second_col_name is the name of second column by which we are going to reorder
Alam Zaib
  • 187
  • 1
  • 11