用雄辩的方式截断laravel中的所有表 [英] truncate all tables in laravel using eloquent
本文介绍了用雄辩的方式截断laravel中的所有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以使用laravel 4中的雄辩或流利的方式截断db中的所有表?我不想指定表名,我只想截断所有表.换句话说,清空所有表格.
Is there a way I could truncate all the tables in a db using eloquent or fluent in laravel 4? I do not want to specify table names, I just want to truncate all the tables. In other words empty all the tables.
推荐答案
注意:
doctrine/dbal
执行此操作需要软件包 操作
NOTE:
doctrine/dbal
Package is Required for Performing this Operations
因此请确保已安装composer require doctrine/dbal
$tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
2.遍历表名数组并使用Schema Builder截断
foreach ($tableNames as $name) {
//if you don't want to truncate migrations
if ($name == 'migrations') {
continue;
}
DB::table($name)->truncate();
}
帮助:如果遇到诸如以下的错误
Help: If you have Got Some Error Such as
SQLSTATE [42000]:语法错误或访问冲突:1701无法截断在外键约束中引用的表
您可以禁用外键检查
Schema::disableForeignKeyConstraints();
并确保重新启用它
Schema::enableForeignKeyConstraints();
这篇关于用雄辩的方式截断laravel中的所有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文