0712-2888027 189-8648-0214
微信公眾號

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號

當(dāng)前位置:主頁 > 技術(shù)支持 > PHP > laravel中檢測表中是否存在索引的函數(shù)代碼

laravel中檢測表中是否存在索引的函數(shù)代碼

時間:2024-09-19來源:風(fēng)信官網(wǎng) 點擊: 1915次
以下這段代碼為laravel中檢測表中是否存在索引的函數(shù),通過該函數(shù)傳遞兩個參數(shù),分別為$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'); 
    
}

使用這個函數(shù)判斷索引相當(dāng)方便。

laravel中檢測表中是否存在索引的函數(shù)代碼
熱門關(guān)鍵詞: laravel 檢測表 索引函數(shù)代碼
欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開