Laravel migration change column name and data type

In this post, I will explain how to change a column name and data type in a Laravel migration. This example will help you understand how to change column types in a Laravel migration. I will provide a simple example of Laravel migration change column name and data type.

How to Rename a Column and Change Its Data Type in Laravel Migration

Renaming columns or changing their data types is a common requirement in any Laravel task. Laravel provides an easy way to rename a column and change its data type in migrations.

How to rename a column in Laravel

To rename a column in Laravel, you can use the renameColumn() method in a migration. This allows you to change the name of an existing column.

How to change a column’s Data Type in Laravel

To change a column’s data type, you can use the change() method. This method allows you to modify the data type of a column in an existing table.

First of all we need to install “doctrine/dbal” composer package.

Install composer package:
composer require doctrine/dbal

Then install composer package we can change data type and change column name using migration. Let’see below example.

Laravel migration change column name example
Migration for main table
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};
Rename column name using migration
Example
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->renameColumn('title', 'name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
        });
    }
};
Changing a columns's Data type in Laravel

To modify a column’s data type, use the change method. Let’s change the body column from text to longText.

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->longText('body')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
        });
    }
};

I hope this tutorial help you.

FAQ

Can I rename a column without losing data in Laravel?

Yes, you can rename a column in Laravel without losing data, provided the new column name is correctly specified in your migration file.

Leave a Reply

Your email address will not be published. Required fields are marked *