needsId) { if (! in_array($node->id, $this->needsId)) { return false; } } $productGroupArray = $node->toArray(); $productGroupArray['isLeaf'] = $node->isLeaf(); if (! $productGroupArray['isLeaf'] && $node->depth < $maxDepth) { $productGroupArray['children'] = $this->getChildNodesArray($node, $maxDepth); if (count($productGroupArray['children']) < 1) { $productGroupArray['isLeaf'] = true; } } return $productGroupArray; } private function getChildNodesArray($node, $maxDepth = 999) { $childrenArray = []; $children = ProductGroup::find($node->id)->descendants()->withDepth()->having('depth', '=', $node->depth + 1)->get(); foreach ($children as $child) { if ($node->depth <= $maxDepth) { if ($children = $this->getNodeToArray($child, true, $maxDepth)) { $childrenArray[] = $children; } } } return $childrenArray; } public function allToTreeArray($NodeId = 1, $maxDepth = 999) { $productGroup = $this->withDepth()->where('id', $NodeId)->get()->first(); return $this->getNodeToArray($productGroup, true, $maxDepth); } public function nodesWithParentToTree(array $nodesId) { $this->needsId = []; foreach ($nodesId as $nodeId) { $this->needsId = array_unique(array_merge($this->needsId, $this->ancestorsAndSelf($nodeId)->pluck('id')->toArray())); } return $this->allToTreeArray(); } /** * add parents Array, parentsId Array, ParentsName Array, hasChild parameter */ public function allWithParents($maxDepth = 999): array { $productGroups = $this->withDepth()->with('ancestors')->get(); $productGroupArray = []; foreach ($productGroups as $item) { if ($item->depth > $maxDepth) { continue; } $item->hasChild = ($item->_rgt - $item->_lft) > 1; $parents = $item->ancestors; $parentNames = $parents->pluck('name')->toArray(); $item->parents = $parents->toArray(); $item->parentsNames = $parentNames; $item->parentsPath = implode('/', $parentNames); $item->parentsIds = $parents->pluck('id')->toArray(); $item->parentsIdPointer = implode('_', $parentNames); $productGroupArray[] = $item->toArray(); } return $productGroupArray; } /** * add parents Array, parentsId Array, ParentsName Array, hasChild parameter */ public function allWithPath($maxDepth = 999): array { $pathSeparator = '/'; $pointerSeparator = '_'; $productGroups = $this->withDepth()->with('ancestors')->get(); $productGroupArray = []; foreach ($productGroups as $item) { if ($item->depth > $maxDepth) { continue; } $item->hasChild = ($item->_rgt - $item->_lft) > 1; $parents = $item->ancestors; $parentNames = $parents->pluck('name')->toArray(); $parentIds = $parents->pluck('id')->toArray(); $item->parentsNames = $parentNames; $item->parentsIds = $parentIds; $item->parentsPath = implode('/', $parentNames); $item->parentsIdPointer = implode('_', $parentNames); if (count($item->parentsIds) > 0) { $item->fullPath = $item->parentsPath.$pathSeparator.$item->name; $item->fullPointer = $item->parentsIdPointer.$pointerSeparator.$item->name; } else { $item->fullPath = $item->name; $item->fullPointer = $item->name; } $productGroupArray[] = $item->toArray(); } return $productGroupArray; } }