table('t_article_type')->get()->sortBy('pid'); $newCategories = []; foreach($oldCates as $cate){ if(empty($cate->name) || empty($dict->valid)){ continue; } $_category = [ 'id' => $cate -> id, 'parent_id' => $cate -> pid ?? 0, 'name' => $cate->name, 'cover' => $cate->icon ?? null, 'sort' => $cate -> sorted, 'is_recommend' => $cate->recommend, 'is_show' => $cate->showInOut, 'lv' => !empty($cate->pid) ? $newCategories[$cate->pid]['lv'] + 1:1, 'path' => !empty($cate->pid) ? $newCategories[$cate->pid]['path'] . $cate->pid .'-':'-', 'created_at' => now(), 'updated_at' => now(), ]; $newCategories[$cate->id] = $_category; } if(count($newCategories) > 0){ DB::table('article_categories')->truncate(); try { DB::begintransaction(); DB::table('article_categories')->insert($newCategories); DB::commit(); } catch (Throwable $th) { DB::rollBack(); report($th); } } } }