How to Create a Migration to Delete a Column in Laravel

How to Create a Migration to Delete a Column in Laravel

Step 1: Generate the Migration

Run the following command to create a migration file for removing a column:

php artisan make:migration delete_column_from_table_name --table=table_name

Step 2: Modify the Migration File

Open the newly created migration file in database/migrations/ and update the up and down methods:

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

public function down()
{
    Schema::table('table_name', function (Blueprint $table) {
        $table->string('column_name'); // Modify the column type as needed
    });
}

Step 3: Run the Migration

Apply the migration to update the database schema:

php artisan migrate

Step 4: Verify the Changes

Check your database to ensure the column has been successfully removed.

Conclusion

Congratulations! You have successfully deleted a column using Laravel migrations.

0 Comments