diff --git a/app/Models/ProductGroup.php b/app/Models/ProductGroup.php index 1db5c77..203f1e7 100644 --- a/app/Models/ProductGroup.php +++ b/app/Models/ProductGroup.php @@ -89,21 +89,25 @@ public function nodesWithParentToTree(array $nodesId) */ public function allWithParents($maxDepth = 999): array { - // $productGroupArray=$this->with('ancestors')->get()->toArray(); + $productGroups = $this->withDepth()->with('ancestors')->get(); + $productGroupArray = []; - $productGroups = $this->withDepth()->get(); foreach ($productGroups as $item) { if ($item->depth > $maxDepth) { continue; } - $children = ProductGroup::descendantsOf($item); - (count($children) > 0) ? $item->hasChild = true : $item->hasChild = false; - $parents = ProductGroup::ancestorsOf($item); + + $item->hasChild = ($item->_rgt - $item->_lft) > 1; + + $parents = $item->ancestors; + $parentNames = $parents->pluck('name')->toArray(); + $item->parents = $parents->toArray(); - $item->parentsNames = ProductGroup::ancestorsOf($item)->pluck('name')->toArray(); - $item->parentsPath = implode('/', ProductGroup::ancestorsOf($item)->pluck('name')->toArray()); - $item->parentsIds = ProductGroup::ancestorsOf($item)->pluck('id')->toArray(); - $item->parentsIdPointer = implode('_', ProductGroup::ancestorsOf($item)->pluck('name')->toArray()); + $item->parentsNames = $parentNames; + $item->parentsPath = implode('/', $parentNames); + $item->parentsIds = $parents->pluck('id')->toArray(); + $item->parentsIdPointer = implode('_', $parentNames); + $productGroupArray[] = $item->toArray(); } @@ -118,19 +122,25 @@ public function allWithPath($maxDepth = 999): array $pathSeparator = '/'; $pointerSeparator = '_'; + $productGroups = $this->withDepth()->with('ancestors')->get(); + $productGroupArray = []; - $productGroups = $this->withDepth()->get(); foreach ($productGroups as $item) { if ($item->depth > $maxDepth) { continue; } - $children = ProductGroup::descendantsOf($item); - (count($children) > 0) ? $item->hasChild = true : $item->hasChild = false; - $parents = ProductGroup::ancestorsOf($item); - $item->parentsNames = ProductGroup::ancestorsOf($item)->pluck('name')->toArray(); - $item->parentsIds = ProductGroup::ancestorsOf($item)->pluck('id')->toArray(); - $item->parentsPath = implode('/', ProductGroup::ancestorsOf($item)->pluck('name')->toArray()); - $item->parentsIdPointer = implode('_', ProductGroup::ancestorsOf($item)->pluck('name')->toArray()); + + $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; diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..8b6abb2 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,44 @@ +in('Feature', 'Unit'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation class at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can register helper functions. +| +*/ + +function something() +{ + // .. +}