generated from panliang/owl-admin-starter
54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Keyword;
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class KeywordSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
Keyword::truncate();
|
|
|
|
$list = [
|
|
['key' => 'score_cate', 'name' => '考核指标', 'children' => [
|
|
['key' => 'score_cate_1', 'name' => '政治忠诚'],
|
|
['key' => 'score_cate_2', 'name' => '政治定力'],
|
|
['key' => 'score_cate_3', 'name' => '政治担当'],
|
|
['key' => 'score_cate_4', 'name' => '政治能力'],
|
|
['key' => 'score_cate_5', 'name' => '政治自律'],
|
|
]],
|
|
['key' => 'banner', 'name' => '广告位', 'children' => [
|
|
['key' => 'banner_1', 'name' => '首页广告'],
|
|
]],
|
|
['key' => 'category', 'name' => '文章分类', 'children' => [
|
|
['key' => 'category_1', 'name' => '共性指标'],
|
|
['key' => 'category_2', 'name' => '进阶指标'],
|
|
]]
|
|
];
|
|
|
|
$this->createByTree($list);
|
|
}
|
|
|
|
protected function createByTree($list, $parent = null)
|
|
{
|
|
foreach ($list as $index => $item) {
|
|
$params = Arr::except($item, ['children']);
|
|
$params['sort'] = $index + 1;
|
|
$params['parent_id'] = $parent ? $parent->id : 0;
|
|
$params['key'] = data_get($item, 'key', data_get($parent, 'key') . '_' . $params['sort']);
|
|
$model = Keyword::create($params);
|
|
|
|
if ($children = data_get($item, 'children')) {
|
|
$this->createByTree($children, $model);
|
|
}
|
|
}
|
|
}
|
|
}
|