Posts Learn Components Snippets Categories Tags Tools new About

How to Rename Column of an Existing Table Migration in Laravel

Learn how to rename columns and change the data type of an existing table in laravel using migration.

7 months ago

5 mins read


In this post, you'll learn how to rename and change the data type of existing columns in Laravel 8 using table migration. The steps are very simple, so let's get started.
For this scenario, imagine you have an existing "Article" model with a corresponding "create_articles_table" migration file and you want to rename the "url" and "synopsis" columns.

Before (Article Model)

  • id
  • url (column to be renamed)
  • synopsis (column to be renamed)
  • title
  • content (previously not nullable)
  • image
  • created_at

After (Article Model)

  • id
  • title
  • slug (newly updated column)
  • summary (newly updated column)
  • content (now can be nullable)
  • image
  • created_at
Now that we can clearly see how the table structure, let's define the migration to rename the new columns.

Step 1: Install Additional Dependency

In order to change/modify the column, you need an additional package dependency called "doctrine/dbal", run the composer command below to require it into the project.
composer require doctrine/dbal

Step 2: Create the Migration

To rename existing columns, you have to run the make migration command but instruct it to modify the existing table. Make use of the "--table=your-table-name" flag and specify the table that you want to modify the columns.
php artisan make:migration rename_url_synopsis_columns_of_articles_table --table=articles
To rename the column you can make use of the "renameColumn" method like below. Pass in the existing column name as the first param and the new column name as the second param.
$table->renameColumn('from', 'to');
On the other hand, to change the column attribute, you can make use of the available modifier and chain "change" method at the end of the previous method. In this case, if the "name" column is not nullable, it will now become nullable.

Final Code

Once the migration class is created, define the necessary changes as follows.

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

class RenameUrlSynopsisColumnsOfArticlesTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::table('articles', function (Blueprint $table) {
            $table->renameColumn('url', 'slug');
            $table->renameColumn('synopsis', 'summary');

Fil Step: Run the Migration

Finally, run the migration command and the 2 columns name will now be renamed and the other column attribute will be updated.
php artisan migrate
By now you will be able to rename and add modify the attribute of a column in few simple steps. If you find this tutorial to be helpful do share it with others and cheers 🍻. happy coding!

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️