/** * Flatten a tree into a non recursive array. * * @param Collection $groupedNodes * @param mixed $parentId * * @return $this */ protected function flattenTree(self $groupedNodes, $parentId) { foreach ($groupedNodes->get($parentId, []) as $node) { $this->push($node); $this->flattenTree($groupedNodes, $node->getKey()); } return $this; }
/** * @param Collection $models * @param int $fixed * @param $parentId * @param int $cut * * @return int */ protected static function reorderNodes(Collection $models, &$fixed, $parentId = null, $cut = 1) { /** @var Node $model */ foreach ($models->get($parentId, []) as $model) { $model->setLft($cut); $cut = self::reorderNodes($models, $fixed, $model->getKey(), $cut + 1); $model->setRgt($cut); if ($model->isDirty()) { $model->save(); $fixed++; } ++$cut; } return $cut; }