Laravel笔记

2022-01-10 09:45:05 浏览数 (1)

1.Laravel在truncate表的时候,如果有外键,先把约束检查关掉再清空表。如:

代码语言:javascript复制
    <code>
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');
    DB::table('members')->truncate();
    DB::table('stores')->truncate();
    DB::table('products')->truncate();
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    </code>

2.递归删除一个文件夹,该文件夹不为空:

/**

  • @param string $dir*
  • @return bool*/

function rmdir_recursive($dir){

代码语言:javascript复制
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $file) {
    if ($file->isDir()) {
        rmdir($file->getRealPath());
    } else {
        unlink($file->getRealPath());
    }
}

return rmdir($dir);

}

0 人点赞