'string', 'pid' => 'string', 'extra' => 'json', 'category'=>'int' ]; /** * 获取导航类型文本 */ public function getTypeTextAttribute(): string { $type = MenuType::tryFrom((int)$this->type); return $type?->label() ?? '未知'; } /** * 获取目标窗口文本 */ public function getTargetTextAttribute(): string { $target = MenuTarget::tryFrom((int)$this->target); return $target?->label() ?? '未知'; } /** * 获取显示状态文本 */ public function getIsShowTextAttribute(): string { $show = YesNoStatus::tryFrom((int)$this->is_show); return $show?->label() ?? '未知'; } /** * 获取状态文本 */ public function getEnabledTextAttribute(): string { $status = EnabledStatus::tryFrom((int)$this->status); return $status?->label() ?? '未知'; } /** * 获取分类文本 */ public function getCategoryTextAttribute(): string { $category = MenuCategory::tryFrom((int)$this->category); return $category?->label() ?? '未知'; } /** * 定义子级关系 * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function children(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(self::class, 'pid'); } /** * 获取所有子级ID(递归) * * @return array */ protected function getAllChildrenIds(): array { $childIds = $this->children()->pluck('id')->toArray(); foreach ($this->children as $child) { $childIds = array_merge($childIds, $child->getAllChildrenIds()); } return array_unique($childIds); } /** * 删除菜单及所有子级 * * @return array|bool 成功删除的ID数组或false */ public function deleteWithAllChildren(): array|bool { $allIds = array_merge([$this->id], $this->getAllChildrenIds()); if (empty($allIds)) { return false; } $successIds = []; // 分块处理以避免内存问题 collect($allIds)->chunk(100)->each(function ($chunk) use (&$successIds) { $chunk->each(function ($id) use (&$successIds) { if ($model = self::find($id)) { if ($model->delete()) { $successIds[] = $id; } } }); }); return !empty($successIds) ? $successIds : false; } }