Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migrations - Dropping columns

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.

like image 816
Black Avatar asked Aug 22 '17 13:08

Black


1 Answers

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.

like image 128
Jerodev Avatar answered Oct 16 '22 13:10

Jerodev