laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

[Need Review & Test] Can create tree with items from different levels

Open oh-bala opened this issue 4 years ago • 2 comments

Phenomenon

With below data structure, if I use toTree, the items indexed with 2 will be eliminated from the result.

$categories = [
  0 => [ "id" => 10068, "parent_id" => 1, "name" => "A"],
  1 => [ "id" => 10085, "parent_id" => 10068, "name" => "B" ],
  2 => [ "id" => 10081, "parent_id" => null, "name" => "D" ]
]

Solution needs to be reviewed

The conditional statement modified to include the nodes (ROOT) has no parent.

/** @var Model|NodeTrait $node */
foreach ($this->items as $node) {
    if ($node->getParentId() == $root || ! $node->getParentId()) {
       $items[] = $node;
    }
}

oh-bala avatar Jan 12 '21 09:01 oh-bala

linked #491

oh-bala avatar Jan 12 '21 09:01 oh-bala

Please do not merge the PR, found some issue with duplicated items, I will fix and create a new patch.

oh-bala avatar Jan 13 '21 00:01 oh-bala