以下这段代码为laravel中检测表中是否存在索引的函数,通过该函数传递两个参数,分别为$table表名,$name字段名,来自动完成检测,示例代码如下:
/**
*检测表中索引是否存在
*$table 表名
*$name 字段名
*/
public function hasIndex($table, $name)
{
$conn = Schema::getConnection();
$dbSchemaManager = $conn->getDoctrineSchemaManager();
$doctrineTable = $dbSchemaManager->listTableDetails($table);
return $doctrineTable->hasIndex($name);
}
if($this->hasIndex('test','test_email_index')){
$table->dropIndex('test_email_index');
}
使用这个函数判断索引相当方便。