Skip to content

Instantly share code, notes, and snippets.

@royteusink
Last active June 15, 2022 07:37
Show Gist options
  • Select an option

  • Save royteusink/d660f9936aed6d3d87f4b85ee63d67c8 to your computer and use it in GitHub Desktop.

Select an option

Save royteusink/d660f9936aed6d3d87f4b85ee63d67c8 to your computer and use it in GitHub Desktop.

Revisions

  1. royteusink revised this gist Jun 15, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions AppServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,9 @@ public function boot()
    /** @var Blueprint $this */

    $schema = Schema::getConnection()->getDoctrineSchemaManager();
    $tableDetils = $schema->listTableDetails($this->getTable());
    $tableDetails = $schema->listTableDetails($this->getTable());

    if ($tableDetils->hasIndex($index)) {
    if ($tableDetails->hasIndex($index)) {
    $this->dropIndex($index);
    }
    });
  2. royteusink renamed this gist Jun 15, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions dropIndexIfExists.php → AppServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    // app/Providers/AppServiceProvider.php

    use Illuminate\Database\Schema\Blueprint;
  3. royteusink created this gist Jun 15, 2022.
    22 changes: 22 additions & 0 deletions dropIndexIfExists.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // 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();
    $tableDetils = $schema->listTableDetails($this->getTable());

    if ($tableDetils->hasIndex($index)) {
    $this->dropIndex($index);
    }
    });
    }

    // Migration usage

    $table->dropIndexIfExists('indexname');