Last active
June 15, 2022 07:37
-
-
Save royteusink/d660f9936aed6d3d87f4b85ee63d67c8 to your computer and use it in GitHub Desktop.
Laravel migration drop index if exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Providers/AppServiceProvider.php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
public function boot() | |
{ | |
Blueprint::macro('dropIndexIfExists', function ($index) { | |
/** @var Blueprint $this */ | |
$schema = Schema::getConnection()->getDoctrineSchemaManager(); | |
$tableDetails = $schema->listTableDetails($this->getTable()); | |
if ($tableDetails->hasIndex($index)) { | |
$this->dropIndex($index); | |
} | |
}); | |
} | |
// Migration usage | |
$table->dropIndexIfExists('indexname'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment