如何在Laravel迁移中检查索引是否存在? [英] How can indexes be checked if they exist in a Laravel migration?

查看:282
本文介绍了如何在Laravel迁移中检查索引是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在准备迁移时尝试检查表上是否存在唯一索引,如何实现?

Trying to check if a unique index exists on a table when preparing a migration, how can it be achieved?

Schema::table('persons', function (Blueprint $table) {
    if ($table->hasIndex('persons_body_unique')) {
        $table->dropUnique('persons_body_unique');
    }
})

类似上面的内容. (显然,hasIndex()不存在)

Something that looks like the above. (apparently, hasIndex() doesn't exist)

推荐答案

使用Laravel使用的"doctrine-dbal"是更好的解决方案:

Using "doctrine-dbal" that Laravel uses is better solution:

Schema::table('persons', function (Blueprint $table) {
    $sm = Schema::getConnection()->getDoctrineSchemaManager();
    $indexesFound = $sm->listTableIndexes('persons');

    if(array_key_exists("persons_body_unique", $indexesFound))
        $table->dropUnique("persons_body_unique");
})

这篇关于如何在Laravel迁移中检查索引是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆