Assume your migration is done and you need to change the column type in Laravel migration. What will you do? There is a quick and beautiful solution for how to change column type in laravel migration. We can do it how to change data type in laravel migration very easily.

To change laravel 9 migration change column type, we will use the doctrine/dbal package. Using this doctrine/dbal package, we can change column type in laravel migration. So in this tutorial, I will show you how to change data type in laravel migration.

So now install this package:

composer require doctrine/dbal

 

After successfully installing the composer package we can change the data type and change the column name using migration. Let's see bellow example:

Migration for the main table:

<?php

use App\Models\User;
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->boolean('is_publish')->default(0);
            $table->timestamps();
        });
    }

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

 

Change Data Type using Migration:

Now change body data type text to long text like:

<?php

use App\Models\User;
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->longText('body')->change();
        });
    }

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

 

Read also: How To Set Max String Length In Laravel Migration?

 

Conclusion

I have tried to discuss the clear concept of laravel migration rename column and change type. Now we know laravel 9 migration change column type. Hope this how to change column type in laravel migration tutorial will help you.