The Schema\Builder class has a hasTable()
and hasColumn()
methods to check the existence of a table and a column, respectively.
Is there any method or way to check if an index key (such as a unique key) exists?
The Schema\Builder class has a hasTable()
and hasColumn()
methods to check the existence of a table and a column, respectively.
Is there any method or way to check if an index key (such as a unique key) exists?
While Laravel doesn't provide any method to check the existence of a key, you could use any of the available queries in MySQL and then use DB::select()
.
For instance:
$keyExists = DB::select(
DB::raw(
'SHOW KEYS
FROM your_table_name
WHERE Key_name=\'your_key_name\''
)
);
Just replace your_table_name
and your_key_name
for the correct values.
doctrine/dbal
's listTableIndexes()
might be a better solution than a DB::raw
call that's MySQL-specific. doctrine-dbal.readthedocs.org/en/latest/reference/…
$conn = Schema::getConnection()->getDoctrineSchemaManager()
Jan 6, 2016 at 15:49
If you are using Laravel then most likely you will have access to an ORM like Eloquent. Assuming you are using Eloquent, you might be able to do something like this:
try {
Schema::table(
'the_name_of_your_table',
function (Blueprint $table) {
$sm = Schema::getConnection()->getDoctrineSchemaManager();
$indexesFound = $sm->listTableIndexes('the_name_of_your_table');
$indexesToCheck = [
'index_name_1',
'index_name_2',
'index_name_3',
'index_name_4'
];
foreach ($indexesToCheck as $currentIndex) {
if (array_key_exists($currentIndex, $indexesFound)) {
// The current index exists in the table, do something here :)
}
}
}
);
} catch (Exception $e) {
}
To get all keys details of a specific table
$keys = DB::select(DB::raw('SHOW KEYS from users'));
foreach($keys as $item)
{
echo $item->Key_name;
}
If you don't know the unique key name but know the table and the column name then here is a example code.
$keys = DB::select(DB::raw('SHOW KEYS from users'));
foreach($keys as $item)
{
if($item->Column_name == 'email')
{
Schema::table('users', function (Blueprint $table) use($item) {
$table->dropUnique($item->Key_name);
});
}
}