Laravel Migrations - Dropping columns

39,526

The down function is used for rollbacks, you have to add this dropColumn in the up function because it is an action you want to perform when running migrations.

So, in your up function there should be:

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

And in the down function you should do the inverse, add the column back:

Schema::table('clients', function (Blueprint $table) {
    $table->string('UserDomainName');
});

This way, you can always return to any point in the migrations.

Share:
39,526
Black
Author by

Black

Full-Stack Webdeveloper Magento 1, Magento 2 & more.

Updated on July 09, 2022

Comments

  • Black
    Black almost 2 years

    I need to drop the column UserDomainName from my database table clients.

    At first I installed doctrine/dbal by executing composer require doctrine/dbal followed by composer update, like described in the documentation.

    Then I created the migration which I want to use to drop the column:

    php artisan make:migration remove_user_domain_name_from_clients --table=clients
    

    I added Schema::dropColumn('UserDomainName'); to the down() method:

    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class RemoveDomainName extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('clients', function (Blueprint $table) {
                //
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('clients', function (Blueprint $table) {
                Schema::dropColumn('UserDomainName');
            });
        }
    }
    

    However, I get

    Migrating: 2017_08_22_135145_remove_user_domain_name_from_clients
    Migrated:  2017_08_22_135145_remove_user_domain_name_from_clients
    

    after executing php artisan migrate but no Column is dropped. If I execute it again I get Nothing to migrate.