*/ public static function forSelect(?int $parentId = null, int $level = 0): Collection { $groups = collect([]); foreach (Group::where('parent_id', $parentId)->orderBy('name')->get()->toBase() as $group) { $groups->push(['name' => str_repeat('- ', $level).$group->name, 'id' => $group->id]); $groups = $groups->merge(static::forSelect($group->id, $level + 1)); } return $groups; } /** * @return DataCollection */ public static function cacheForSelect(): DataCollection { return static::collection(static::forSelect()); } }