20
common/Database/Traits/AddsIndexToExistingTable.php
Executable file
20
common/Database/Traits/AddsIndexToExistingTable.php
Executable file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Database\Traits;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
trait AddsIndexToExistingTable
|
||||
{
|
||||
protected function addIndexIfDoesNotExist(Blueprint $table, string $column) {
|
||||
$prefix = Schema::getConnection()->getTablePrefix();
|
||||
$sm = Schema::getConnection()->getDoctrineSchemaManager();
|
||||
$tableName = "{$prefix}{$table->getTable()}";
|
||||
$indexesFound = $sm->listTableIndexes($tableName);
|
||||
if (!array_key_exists("{$tableName}_{$column}_index", $indexesFound)) {
|
||||
$table->index($column);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user