Nếu bạn đang sử dụng Laravel thì rất có thể bạn sẽ có quyền truy cập vào ORM như Eloquent. Giả sử bạn đang sử dụng hùng hồn, bạn có thể làm điều gì đó như thế này:
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) {
}
Nguồn
2017-04-12 14:10:50
Laravel sử dụng Học thuyết tại các điểm (đặc biệt là Schema), do đó 'doctrine/dbal' của' listTableIndexes() 'có thể là một giải pháp tốt hơn hơn một cuộc gọi 'DB :: raw' cụ thể là MySQL. http://doctrine-dbal.readthedocs.org/en/latest/reference/schema-manager.html – ceejayoz
@ceejayoz Cảm ơn vì điều đó. Tôi đã học được điều gì đó ngày hôm nay :) –
@ceejayoz Để có được Doctrine Manager trong Laravel: '$ conn = Schema :: getConnection() -> getDoctrineSchemaManager()' – shrimpwagon