djc-new/database/seeders/ArticleCategorySeeder.php

56 lines
1.6 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Throwable;
class ArticleCategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//老文章分类表
$oldCates = DB::connection('djc_mysql')->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);
}
}
}
}