How to Change a Table Column Type Using Laravel Migrations

As you develop your Laravel application, you may encounter situations where you need to change the data type of specific columns. Thankfully, Laravel allows us to change the column type in a table, using migrations, while keeping existing data intact.

In this tutorial, we will guide you through the process of changing the column type of a “description” column, from varchar(255) to text to increase the maximum length it can contain from 255 to 65,535 characters.

Let’s get started!

Step 1: Add Doctrine/DBAL Package (Can Skip For Laravel 10+)

Before we proceed, we need to check your Laravel version. Should it be Laravel 9 or earlier you will need to manually install the Doctrine/DBAL package, using composer. Otherwise, you would see an exception message when attempting to change a column’s data type.

Open your terminal and navigate to your project’s root folder, then run the following command:

php artisan --version

Should it show version 10 or higher, as shown below, you are good to go and can go to step 2.

The output of Running: php artisan --version

However, if it shows Laravel version 9 or lower you will need to run:

composer require doctrine/dbal

Now, we’ve ensured Laravel supports changing column types.

Step 2: Create a Migration

Now let’s generate a migration file for the “movies” table by running the following command:

php artisan make:migration modify_movies_table --table=movies

This will create a new migration file specifically for the “movies” table.

Step 3: Edit the Migration File and Add change() Method Call

Open the generated migration file and edit the up() and down() methods, to match the following code:

database/migrations/2023_06_09_193152_modify_movies_table.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('movies', function (Blueprint $table) {
            // Change column type to text
            $table->text('description')->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('movies', function (Blueprint $table) {
            // Revert the column type change
            $table->char('description')->change();
        });
    }
};

Using the code from this example, we can modify the “description” column of the “movies” table from the type char to text by using the ->change() method.

When writing migrations it is good practice to properly implement the down() method as well. This ensures that the action can be reverted by using php artisan migrate:rollback if necessary.

Step 4: Run the Migration

To run the migration and apply the changes to your database, run the following command:

php artisan migrate

Laravel will then execute the migration, and the column type of the “description” column in the “movies” table will be changed to text.

That’s it! We’ve successfully changed our column type to “text”.

Note that not all types can be converted like shown here. Read on on how to alter column types which are not supported by the ->change() method.

Changing a Column Type Using Raw SQL in a Migration

Laravel’s ->change() method doesn’t support all types of columns. In any case, we can still write raw SQL Query to use any types that are supported by your database.

Consider the code example below, where we change the column “file_binary_data” from a BLOB type to a LONGBLOB using raw SQL:

database/migrations/2023_06_09_201137_modify_documents_table.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('documents', function (Blueprint $table) {
            DB::statement("ALTER TABLE {$table->getTable()} MODIFY `file_binary_data` LONGBLOB");
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('documents', function (Blueprint $table) {
            DB::statement("ALTER TABLE {$table->getTable()} MODIFY `file_binary_data` BLOB");
        });
    }
};

Conclusion

In this tutorial, we covered how to change the type of a column by using Laravel migrations.

We learned that when running Laravel 9 and older, we first need to install the Doctrine/DBAL package manually before we can change column types. We also learned how to add raw ALTER TABLE statements into the migration’s up and down methods, as a fallback method in case Laravel does not support the column type you need.

With this knowledge, you can confidently modify column types in your Laravel applications, adapting your database structure to meet the evolving needs of your project.

Happy coding!

Johan van den Broek

Johan is the creator of laracoding.com. As a child, he began tinkering with various programming languages, many of which have been long forgotten today. Currently, he works exclusively with PHP and Laravel, and his passion for programming remains to this day.

Leave a Reply

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

Recent Posts