Laravel Add New Column to Existing Table in a Migration

April 7, 2023
Laravel
Laravel Add New Column to Existing Table in a Migration

In this tutorial, you will learn How to add a new column to an existing table using laravel migration.

If you are doing development, chances are that sometimes you or me forget to add the column that needs to add to an existing table. Thanks to laravel migration, you can easily add a new column to an existing table in laravel migration.

Assumption

For this example, we will be considering the example of users, where in the users table we want to add description with type of longText.

Lets get Started!

Step 1: Install Laravel

The first step is to install the laravel. I am installing laravel in the add_column folder. Here is the command on how to install laravel:

composer create-project --prefer-dist laravel/laravel add_column

Step 2: Add DB Credentials into Env file

Now in this step, you need to add the DB credentials into .env file.

So Simply go to the .env file in the main directory and change DB_PORTDB_DATABASEDB_USERNAME and DB_PASSWORD variables with your DB credentials.

Step 3: Migrate Database

Now in this step, we need to migrate the Database, so that we can push the default tables into the database. Type the following command in the terminal:

php artisan migrate

Now till this point, all the default tables will get created in the database.

Image of Default Tables get created into database using laravel migration

Step 4: Create New Migration for Adding New Column

Since we want to add the new column description in the users table with the longText datatype.

So for that, we need to create the migration file. Now you can name it anything you want but I will name it add_new_column_to_users_table. So type the following command in the terminal:

php artisan make:migration add_new_column_to_users_table

This will create a new migration file in database/migrations with the name of add_new_column_to_users_table.

Step 5: Edit the Migration file

In this step, we need to edit the migration file. So if go to the database/migrations and open migration file add_new_column_to_users_table, you will see similar following code:

<?php

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

class AddNewColumnToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //

    }

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

Step 6: Add the Column in the Up function

In this step, we will add the code for adding the column into table, change the up function from the above file and add the column description with datatype longText into users table. Here is what your final up function code will look like:

public function up()
    {
Schema::table('users', function (Blueprint $table) {
           $table->longText('description');
        });    
}

In the above code for the up function, first we added the laravel code for selecting the table users using Schema::table and inside its function, we are creating the column description with the datatype longText.

Step 7: Run the migrate command

Now we need to migrate once again, so that the change we made into the users table get into the database. Type the following command into the terminal:

php artisan migrate

If you are getting an error that you cannot change the table. Follow the next step, otherwise you can see in the database, the column would be successfully added.

Step 8: Install DABL Package if you getting Exception or Error

Now sometimes when you are changing the table itself, it gives you Exception or Error. That you cannot change the table unless you install doctrine/dbal package.

If you getting this error don’t worry, type the following command into the terminal to install doctrine/dbal package:

composer require doctrine/dbal

It is also mentioned in laravel documentation that you need to save the state before changing the column. If you are not facing any error on add the new column then its not required to install doctrine/dbal.

That was it on how to add a new column to an existing table in laravel migration.

Conclusion:

In the above article, we have discussed how to add a new column to an existing table using laravel migration.

Also check this article, if you want to change the column name using laravel migration.

Let me know if you have any questions. I will see you in the next one 🙂

Write a Reply or Comment

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


Icon