How to drop multiple columns from the table using Laravel migration?

By | May 18, 2021

Laravel gives us the flexibility to add or remove columns from the database table easily without getting access of database server. You can just simply create a migration file to drop multiple columns and run it on your your server, that’s it.

However, It is always recommended that you should have both Up and Down method in your migration file therefore, in critical situation you can rollback changes.

Drop Multiple Columns Using Laravel Migration

I have created a migration file using Laravel version 8 to demonstrate you how you can also create a migration to drop multiple columns from the table.

<?php

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

class Members extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $table) {
			$table->dropColumn([
                           'address2', 
                           'email_id', 
                           'surname'
                         ]); // add any number of columns that you want to remove.
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::create('members', function (Blueprint $table) {
           $table->string('address2');
		   $table->string('email_id');
		   $table->string('surname');
        });
    }
}

Drop Single Column

If you are looking to drop a single column then you can create your migration file like this.

<?php

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

class Members extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            $table->dropColumn('address2');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::create('members', function (Blueprint $table) {
           $table->string('address2');
        });
    }
}

Hope, this small guide will help you.