Laravel Change Table or Column Name with Data Type Tutorial

Last updated on by Digamber

This is a step by step guide on Laravel 8 Migration about changing or transforming data type on top of that column name using migration in Laravel application with the help of doctrine/dbal package.

The DBAL is a database abstraction layer especially developed for robust PHP database abstraction layer (DBAL) processing. As the name suggests, it comes with intuitive and sturdy features for database schema introspection over and above for management.

Handling database is frequent work for laravel developers; you face needless issues while working with it. However, fret not; in this tutorial, you will learn how to profoundly change the data type and update the column name using migration in laravel.

This tutorial is created considering Laravel 8, although you can use the similar approach for the previous versions such as Laravel 7 and 6. You will have the answer for how to change the data type and column name in the Laravel application.

Create Laravel Application

First, install a fresh laravel application:

composer create-project laravel/laravel --prefer-dist laravel-example-app

Create Model

It would be best to create a new model over and above that executes migration to insert values into the database. However, it seems a needless step though it is needed so that you can see how the data type and column name is updated after.

php artisan make:model Student -m

Add the table values for migration in migrations/timestamp_create_students_table file:

<?php

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

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('gender');
            $table->string('email');
            $table->string('dob');
            $table->timestamps();
        });
    }

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

Run laravel migration command:

php artisan migrate

Value migrated to the database:

Laravel Change Table or Column Name with Data Type Tutorial

Install DBAL Package

Now, you will see how to change the table or column name with Laravel migration however first you need to install the “Database Abstraction Layer” package with composer command:

composer require doctrine/dbal

Now, you are all set to change the data type, in the same way, you will rename the column name using migration in Laravel project.

Rename Laravel Column Name with Migration

You don’t need to rely on needless tantrums for changing or renaming the column Name with migration in Laravel.

Change column name “gender” to “subjects” so run command to create a new migration file:

php artisan make:migration rename_gender_in_students_table --table=students

Add the code in newly created rename_gender_in_students_table.php file:

<?php

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

class RenameGenderInStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('students', function (Blueprint $table) {
            $table->renameColumn('gender', 'subjects');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('students', function (Blueprint $table) {
            $table->renameColumn('gender', 'subjects');
        });
    }
}

Execute command to change or rename the column value gender to subjects:

php artisan migrate

rename the column value

Update Data Type with Laravel Migration

Updating or changing data type is a facile task with laravel migration:

<?php

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

class RenameGenderInStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('students', function (Blueprint $table) {
            $table->text('subjects')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

    }
}

It’s been my pleasure sharing this tutorial with you, i reckon you would love this tutorial not only – but also share this small laravel migration guide with others.