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?
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?
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");
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>");
}