Laravel Change Table or Column Name with Data Type

Reading Time: 6 minutes
1,001 Views

Inside this article we will see the concept i.e Laravel Change Table or Column Name with Data Type Tutorial. Article contains the classified information about How to Change Column Type using Laravel Migration.

When we explore application in some advanced features then at some point we need to alter table and it’s properties. So, here in this short tutorial we will see how to update column name, data type of column in laravel using migration concept.

Read More: Convert CSV File Data To JSON String Using PHP Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create a Migration File

Let’s consider a migration file for this.

Open project into terminal and run this command to create a students migration file for students table.

$ php artisan make:migration create_students_table

It will create a file i.e xxx_create_students_table.php inside /database/migrations folder.

<?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('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');
    }
};

Next, run this command.

$ php artisan migrate

Read More: How To Drop Column From Migration in Laravel Tutorial

Once you migrate, it will create students table in your database.

To work with the features of updating table schema we need to install a composer package inside application.

Install DBAL Package

Back to project terminal and run this 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

Let’s change column name “gender” to “subjects”.

Back to terminal and run this command.

$ php artisan make:migration rename_gender_in_students_table --table=students

It will create a file i.e xxx_rename_gender_in_students_table.php inside /database/migrations folder.

Read More: Laravel Migration with Default Current Timestamp Tutorial

Open file and write this complete code into it.

<?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('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');
        });
    }
};

Next, run this command.

$ php artisan migrate

Once you migrate, it will update the column name from gender to subjects in students table in your database.

Update Data Type with Laravel Migration

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

Concept of code

Schema::table('students', function (Blueprint $table) {
    $table->text('subjects')->change();
});

In migration file, you need to write

<?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('students', function (Blueprint $table) {
            $table->text('subjects')->change();
        });
    }

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

Next, run this command.

$ php artisan migrate

Once you migrate, it will update the update the data type of subjects column in students table in your database.

Read More: Find NameServers of Domain Using PHP Shell Command

We hope this article helped you to learn about Laravel Change Table or Column Name with Data Type Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.