Skip to content

Instantly share code, notes, and snippets.

@royteusink
Last active June 15, 2022 07:37
Show Gist options
  • Save royteusink/d660f9936aed6d3d87f4b85ee63d67c8 to your computer and use it in GitHub Desktop.
Save royteusink/d660f9936aed6d3d87f4b85ee63d67c8 to your computer and use it in GitHub Desktop.
Laravel migration drop index if exists
<?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