How to add new columns to the existing table in Laravel migration

Laravel 10 Apr 2019

Sometimes it is required to add new columns in Laravel tables. Today I will show you how to add new column or columns to the existing table in Laravel migration.

Add a single Column:

To add a new column to the existing table in Laravel, you need to add the following command in terminal.

php artisan migrate:make add_profile_to_users

It will create a add_profile_to_users file in migration folder. That should be like this-

class AddProfileToUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('profile')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('shop_users', function (Blueprint $table) {
            $table->dropColumn(['profile']);
        });
    }
}

Add multiple columns

If you need to add multiple columns, you need to add the following command in the terminal.

php artisan migrate:make add_profile_and_bio_to_users

It will generate a add_profile_to_users file in migration folder. That contains the following link-

class AddProfileToUsers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('profile')->nullable();
            $table->string('bio')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('shop_users', function (Blueprint $table) {
            $table->dropColumn(['profile',  'bio']);
        });
    }
}

Hope it will work for you.