table('cases')->get(); $categoryArr = [ 0=> 'examples', 1=> 'services', 2=> 'news', ]; foreach($oldArticles as $article){ $_article = [ 'id' => $article -> id, 'category' => $categoryArr[$article -> type ?? 0], 'title' => $article->title, 'cover' => $article->img, 'content' => $article->content, 'published_at' => now(), 'sort' => $article -> sort ?? 0, 'is_recommend' => $article->recommend, 'is_enable' => 1, 'created_at' => now(), 'updated_at' => now(), ]; $articles[] = $_article; } if(count($articles) > 0){ DB::table('articles')->truncate(); try { DB::begintransaction(); DB::table('articles')->insert($articles); DB::commit(); } catch (Throwable $th) { DB::rollBack(); report($th); } } } }