Se si utilizza Laravel, molto probabilmente si avrà accesso a un ORM come Eloquent. Supponendo che si sta utilizzando Eloquente, si potrebbe essere in grado di fare qualcosa del genere:
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) {
}
fonte
2017-04-12 14:10:50
laravel utilizza Doctrine in punti (in particolare schema), in modo da 'doctrine/di dbal'' listTableIndexes() 'potrebbe essere una soluzione migliore di una chiamata 'DB :: raw' che è specifica per MySQL. http://doctrine-dbal.readthedocs.org/en/latest/reference/schema-manager.html – ceejayoz
@ceejayoz Grazie per quello. Ho imparato qualcosa oggi :) –
@ceejayoz Per ottenere Doctrine Manager in Laravel: '$ conn = Schema :: getConnection() -> getDoctrineSchemaManager()' – shrimpwagon