Luí*_*ruz 15
虽然Laravel没有提供任何方法来检查密钥的存在,但您可以使用MySQL中的任何可用查询然后使用DB::select().
例如:
$keyExists = DB::select(
DB::raw(
'SHOW KEYS
FROM your_table_name
WHERE Key_name=\'your_key_name\''
)
);
Run Code Online (Sandbox Code Playgroud)
只需替换your_table_name并your_key_name获取正确的值.
如果您使用的是Laravel,那么很可能您可以访问像Eloquent这样的ORM.假设您正在使用Eloquent,您可以执行以下操作:
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) {
}
Run Code Online (Sandbox Code Playgroud)