2

I am editing a table so that it will use polymorphic relationships:

public function up()
    {
        Schema::table('locations', function (Blueprint $table) {
            $table->morphs('location');
        });
    }

But I do not know the best way appropriate to reverse this migration. Would I have to drop the two columns that it creates and the index itself, or is there a way of doing this in one line in Laravel? Thanks.

party-ring
  • 1,761
  • 1
  • 16
  • 38

2 Answers2

11

Found this in the Blueprint api:

public function dropMorphs($name, $indexName = null)
{
    $this->dropIndex($indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]));

    $this->dropColumn("{$name}_type", "{$name}_id");
}

So just $table->dropMorphs('location');

Jeff
  • 24,623
  • 4
  • 69
  • 78
1

If anyone comes across this post while working on the same issue, only with an SQLite DB like me, SQLite doesn't support dropping the columns and index in one go. You'll need to do it manually, and split it out in multiple modifications like so:

public function down(){
    Schema::table('locations', function (Blueprint $table) {
        $table->dropcolumn('location_id');
    });

    Schema::table('locations', function (Blueprint $table) {
        $table->dropcolumn('location_type');
    });
}