58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Keyword;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class KeywordSeeder extends Seeder
|
|
{
|
|
public $id = 1;
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
Keyword::truncate();
|
|
$list = [
|
|
['key' => 'treat_type', 'name' => '诊疗类别', 'children' => [
|
|
['key' => 'treat_head', 'name' => '头疗', 'content' => '<p>按摩意见:</p><p>服务意见:</p><p>效果意见:</p>'],
|
|
['key' => 'treat_normal', 'name' => '看病', 'content' => ''],
|
|
]]
|
|
];
|
|
$this->createByTree($list);
|
|
}
|
|
|
|
protected function createByTree($list, $parent = null)
|
|
{
|
|
$count = count($list);
|
|
foreach ($list as $index => $item) {
|
|
$key = data_get($item, 'key');
|
|
if (! $key) {
|
|
if (! $parent) {
|
|
throw new \Exception('key 必填');
|
|
}
|
|
$key = $parent->key.'_'.($index + 1);
|
|
}
|
|
$attributes = [
|
|
'id' => data_get($item, 'id', $this->id),
|
|
'key' => $key,
|
|
'parent_id' => data_get($parent, 'id', 0),
|
|
'path' => ($parent ? $parent->path.$parent->id : '').'-',
|
|
'type_key' => data_get($parent, 'key'),
|
|
'level' => data_get($parent, 'level', 0) + 1,
|
|
'sort' => $count - $index,
|
|
];
|
|
|
|
$model = Keyword::create(array_merge($attributes, Arr::except($item, ['children'])));
|
|
$this->id++;
|
|
if ($children = data_get($item, 'children')) {
|
|
$this->createByTree($children, $model);
|
|
}
|
|
}
|
|
}
|
|
}
|