When you create a new column in a table you can use the ->after(\'column name\') to dictate where it goes. How can I create a migration that re-orders the columns in the right o
Very Important Note
Use the following solution only if you still in the development phase and you haven't launched your app yet as the following solution will delete the column and all data stored in it and will create a new empty column with the same name after the column you determine.
Suppose your column name is address
and you want to reorder its position so that it comes after another column called city
, and your table name is employees
.
In your terminal type the next command:
php artisan migrate:make reorganize_order_of_column_address --table=employees
You may only change reorganize_order_of_column_address
and employees
according to your needs, but keep the rest of the command as it is.
This will generate a migration file in app/database/migrations
folder, open it and put your code inside the up()
function like this:
public function up()
{
Schema::table('employees', function(Blueprint $table)
{
$table->dropColumn("address");
});
Schema::table('employees', function(Blueprint $table)
{
$table->string('address')->after("city");
});
}