Rename a column in a Laravel migration
August 3, 2022 ‐ 1 min read
To rename a database column in Laravel we need a database migration.
The method we want for this is renameColumn()
which takes takes the current column name as its first parameter and the new preferred column name as a second parameter.
<?php
class RenameCurrentNameColumn extends Migration
{
public function up()
{
Schema::table('tasks', function(Blueprint $table) {
$table->renameColumn('current_name', 'new_name');
});
}
public function down()
{
Schema::table('tasks', function(Blueprint $table) {
$table->renameColumn('new_name', 'current_name');
});
}
}
Obviously renaming the column does require you to change the references in your code. If changing all the references is too much work, an accessor might make your life easier.
Note: This action does require you to have the dependency
doctrine/dbal
installed.