Skip to main content
PHP Blog

Back to all posts

How to Drop Columns In Laravel Migration?

Published on
8 min read
How to Drop Columns In Laravel Migration? image

Best Laravel Migration Tools to Buy in October 2025

1 Laravel: Up & Running: A Framework for Building Modern PHP Apps

Laravel: Up & Running: A Framework for Building Modern PHP Apps

BUY & SAVE
$41.38 $55.99
Save 26%
Laravel: Up & Running: A Framework for Building Modern PHP Apps
2 Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)

Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)

BUY & SAVE
$15.13 $22.99
Save 34%
Mastering the Snowflake SQL API with Laravel 10: A Comprehensive Guide to Data Cloud Integrated Development (Apress Pocket Guides)
3 Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer

Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer

BUY & SAVE
$5.99
Laravel Essentials: Tips & Tricks for Developers: Master Laravel with Practical Tips for Every Developer
4 Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)

Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)

BUY & SAVE
$0.99
Architecture of complex web applications. Second Edition.: With examples in Laravel(PHP)
5 Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)

Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)

BUY & SAVE
$2.99
Laravel 7.X : LEARN BASIC LESSONS & BUILD A CRUD APP (PHP Framework)
6 Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease

Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease

BUY & SAVE
$39.00
Consuming APIs in Laravel: Build Robust and Powerful API Integrations For Your Laravel Projects With Ease
7 Overview Of Laravel PHP Framework: For Other Web Framework Users

Overview Of Laravel PHP Framework: For Other Web Framework Users

BUY & SAVE
$2.99
Overview Of Laravel PHP Framework: For Other Web Framework Users
8 The Laravel Survival Guide: Written & Updated for Laravel 5.3

The Laravel Survival Guide: Written & Updated for Laravel 5.3

BUY & SAVE
$9.99
The Laravel Survival Guide: Written & Updated for Laravel 5.3
+
ONE MORE?

To drop columns in Laravel migration, you need to follow these steps:

  1. Open your terminal and navigate to your Laravel project.
  2. Generate a migration file using the make:migration artisan command. For example, let's say you want to drop a column named email from the users table. You can run the following command: php artisan make:migration drop_email_column_from_users_table --table=users This will create a new migration file in the database/migrations directory.
  3. Open the generated migration file and locate the up method. Inside this method, you can use the dropColumn method to remove the desired column. In this case, you want to drop the email column, so your code should look like this: public function up() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('email'); }); }
  4. If you also want to add the column back in the future, you can implement the down method to reverse the column removal. This would look like: public function down() { Schema::table('users', function (Blueprint $table) { $table->string('email')->nullable(); // Add nullable to match the original column definition }); } Note that the down method is optional; it enables the rollback functionality if needed.
  5. Save the migration file and run the migration using the migrate artisan command: php artisan migrate This will execute the up method of the migration, dropping the specified column from the table.

That's it! The column email will be dropped from the users table in your Laravel application.

How to drop a column with default value in Laravel migration?

To drop a column with default value in a Laravel migration, you can use the change method along with the default method. Here is an example:

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

class DropColumnWithDefaultValue extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('your_table_name', function (Blueprint $table) { // Drop the column with default value $table->dropColumn('column_name');

    });
}

/\*\*
 \* Reverse the migrations.
 \*
 \* @return void
 \*/
public function down()
{
    Schema::table('your\_table\_name', function (Blueprint $table) {
        // Recreate the column with default value
        $table->integer('column\_name')->default(0);
    
    });
}

}

In the up method, you use the dropColumn method to remove the column with the given name. And in the down method, you can use the default method to set the default value for the column when rolling back the migration.

Replace 'your_table_name' with the actual name of your table and 'column_name' with the name of the column you want to drop.

How to drop a column in Laravel migration?

To drop a column in Laravel migration, you can use the dropColumn method provided by the Laravel Schema Builder.

Here's an example of how to drop a column in a Laravel migration:

  1. Create a new migration file by running the following command in your terminal:

php artisan make:migration drop_column_from_table --table=table_name

Replace drop_column_from_table with an appropriate name for your migration and table_name with the name of the table from which you want to drop the column.

  1. Open the newly created migration file located in the database/migrations directory.
  2. Inside the up method, add the following code to drop the column:

public function up() { Schema::table('table_name', function (Blueprint $table) { $table->dropColumn('column_name'); }); }

Replace table_name with the name of your table and column_name with the name of the column you want to drop.

  1. If you want the ability to rollback the migration, you can add the opposite operation in the down method like this:

public function down() { Schema::table('table_name', function (Blueprint $table) { $table->addColumn('data_type', 'column_name')->nullable(); }); }

Replace data_type with the appropriate data type for the column you had dropped.

  1. Save the file and run the migration by executing the following command in your terminal:

php artisan migrate

The column specified in the migration file will be dropped from the specified table in your database.

How to drop a column if it exists in Laravel migration?

To drop a column if it exists in Laravel migration, you can use the dropIfExists method. Here's an example:

Schema::table('your_table_name', function($table) { if (Schema::hasColumn('your_table_name', 'column_name')) { $table->dropColumn('column_name'); } });

In the above example, we check if the specified column (column_name) exists in the table (your_table_name) using Schema::hasColumn. If it exists, we then drop the column using the dropColumn method.

Make sure to replace 'your_table_name' with the actual name of your table and 'column_name' with the actual name of the column you want to drop.

This code should be placed inside a migration file and run using the php artisan migrate command to execute the migration.

How to drop a column based on its position rather than name in Laravel migration?

To drop a column based on its position rather than its name in a Laravel migration, you can use the Doctrine\DBAL\Schema\Blueprint class to access the underlying table definition and drop the column using its position.

Here's an example of how you can achieve this:

  1. Start by creating a new migration using the artisan command:

php artisan make:migration drop_column_based_on_position --table=your_table_name

  1. Open the newly created migration file, which will be located in the database/migrations directory. Inside the up function, add the following code:

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

class DropColumnBasedOnPosition extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('your_table_name', function (Blueprint $table) { // Get the underlying Doctrine\Table object $doctrineTable = $table->getTable();

        // Get the columns in the table
        $columns = $doctrineTable->getColumns();

        // Get the column at the desired position (e.g., column at position 3)
        $columnToDrop = array\_values($columns)\[2\];

        // Drop the column from the table
        $table->dropColumn($columnToDrop->getName());
    });
}

/\*\*
 \* Reverse the migrations.
 \*
 \* @return void
 \*/
public function down()
{
    // This is the "reverse" migration method and can be left empty
}

}

In the code snippet above, replace 'your_table_name' with the actual name of the table you want to modify. The line $columnToDrop = array_values($columns)[2]; assumes that you want to drop the column at position 3 (0-based indexing). Change the index number to the desired position of the column you want to drop.

  1. After saving the migration file, run the migration using the artisan command:

php artisan migrate

This will execute the migration and drop the specified column from the table based on its position.

Please note that dropping a column based on its position can be risky, especially if the table structure changes in the future. It's generally recommended to use column names for more reliable and maintainable migrations.

What is the syntax for dropping a column in Laravel migration?

The syntax for dropping a column in Laravel migration is as follows:

Schema::table('table_name', function (Blueprint $table) { $table->dropColumn('column_name'); });

Replace table_name with the name of your database table and column_name with the name of the column you want to drop.

How to drop a column using Artisan CLI command in Laravel?

To drop a column in Laravel using the Artisan CLI command, you can follow these steps:

  1. Open the command line or terminal and navigate to your Laravel project directory.
  2. Run the following command to generate a migration file that will contain the code to drop the column:

php artisan make:migration drop_column_name_from_table_name_table --table=table_name

Replace column_name with the name of the column you want to drop, and table_name with the name of the table from which you want to drop the column.

  1. This command will generate a new migration file in the database/migrations directory. Open the generated file and locate the up() method.
  2. Within the up() method, use the Schema Builder's table() method to drop the column.

For example, if you want to drop a column named email from a table named users, the code in the up() method would be:

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

  1. Save the changes to the migration file and close it.
  2. Run the migration using the following command:

php artisan migrate

This command will execute the migration and drop the specified column from the table.

Note: Make sure to replace column_name and table_name in the commands with your actual column and table names.

How to drop a unique constraint on a column in Laravel migration?

To drop a unique constraint on a column in Laravel migration, you can use the dropUnique method in the Schema facade.

Here's an example of how you can use it:

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

class DropUniqueConstraintMigration extends Migration { /** * Run the migration. * * @return void */ public function up() { Schema::table('your_table_name', function (Blueprint $table) { $table->dropUnique('your_column_unique_constraint_name'); }); }

/\*\*
 \* Reverse the migration.
 \*
 \* @return void
 \*/
public function down()
{
    Schema::table('your\_table\_name', function (Blueprint $table) {
        $table->unique('your\_column\_name', 'your\_column\_unique\_constraint\_name');
    });
}

}

In the up method, you can use the dropUnique method to drop the unique constraint. Make sure to pass the name of the unique constraint as the parameter.

In the down method, you can use the unique method to recreate the unique constraint.

Note: Replace 'your_table_name', 'your_column_name', and 'your_column_unique_constraint_name' with the actual table name, column name, and unique constraint name respectively.