'treat_type', 'name' => '诊疗类别', 'children' => [ ['key' => 'treat_head', 'name' => '头疗', 'content' => '按摩意见:'.PHP_EOL.'服务意见:'.PHP_EOL.'效果意见:'.PHP_EOL, 'options' => [ 'doctor_ratio' => 10, 'inviter_ratio' => 9, 'saler_ratio' => 8 ]], ['key' => 'treat_normal', 'name' => '看病', 'content' => '病症:', 'options' => [ 'doctor_ratio' => 10, 'inviter_ratio' => 10, 'saler_ratio' => 10 ]], ]], ['key' => 'illness_type', 'name' => '病种', 'children' => [ ['name' => '糖尿病'], ['name' => '高血压'], ['name' => '腰间盘突出'], ]] ]; $this->createByTree($list); // 添加类别权限 $service = (new KeywordService()); foreach (Keyword::where('type_key', 'treat_type')->get() as $item) { $service->afterCreate($item); } } 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 = [ '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, ]; if ($id = data_get($item, 'id')) { $attributes['id'] = $id; } $model = Keyword::create(array_merge($attributes, Arr::except($item, ['children']))); if ($children = data_get($item, 'children')) { $this->createByTree($children, $model); } } } }