How to Rename Column Name in Laravel using Migration

Using the Laravel change column name example tutorial we will show you how to rename a database column name using migration in laravel.

In laravel Modify the table like renaming column, change data type column to existing table laravel provide us separate package doctrine/dbal. In laravel migration we can rename colum to existing table using the method renameColumn().

The laravel migration have up and down methods where the up methods update the column name in database and the down methods revoke that. You can use to change the column name and datatype in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

Migration of the Existing table:

Suppose you have a previous created database migration something like this, and you want to change the notes column with description so you need to add new migration and change the column see below steps:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->datetime('start_date')->nullable();
            $table->datetime('end_date')->nullable();
            $table->text('notes')->nullable();
            $table->string('report_to')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projects');
    }
}

Renaming any column name we need to create a new migration which is not show any error in future.

Create Migration

So, renaming or changing the database column name you need to create a new migration using to run the following command in your terminal.

php artisan make:migration rename_notes_to_description_in_projects_table --table=projects

After running the command its generated a new migration in your database>migration directory, open the file and below code on it.

The up method change column notes to description.

    public function up()
    {
        Schema::table('tasks', function (Blueprint $table) {
             $table->renameColumn('notes', 'description');
        });
    }

Now we need to add a rename code in down methods. Because when we run php artisan migrate:rollback command then its change ‘description’ to notes.

    public function down()
    {
        Schema::table('tasks', function (Blueprint $table) {
            $table->renameColumn('description', 'notes');
        });
    }

After adding these two method’s code now run migration command.

php artisan migrate

Install doctrine/dbal package

Laravel changing the database column or datatype we need to doctrine/dbal package in our laravel app; So if you have not installed previously then run the following command;

composer require doctrine/dbal

Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

So, friends laravel change column name using migration is over now, You can check in the database to check the column name is change now.

3 thoughts on “How to Rename Column Name in Laravel using Migration”

Leave a Comment