/** * Update node. * * @param int $node * @return \Illuminate\Database\Eloquent\Model * * @throws \Langaner\MaterializedPath\Exceptions\NodeNotFoundException */ public function updateNode(Model $node) { if (!$node) { throw new NodeNotFoundException('Node doesnt exist'); } $parent = $this->newQuery()->find($node->parent_id); if ($parent === null) { $parent = $this->createRootObject(); } $node->sibling()->where($this->getColumnTreeOrder(), '>=', $node->getTreeOrder())->where('id', '!=', $node->id)->increment($this->getColumnTreeOrder()); $children = $node->childrenByDepth()->get(); if ($parent->getTreeRealPath() === '') { $path = $this->separator . '0' . $this->separator; if ($this->useRealPath()) { $realPath = $node->{$this->columnAlias}; } } else { $path = str_replace($node->getTreePath(), $parent->getTreePath() . $parent->id . $this->separator, $node->getTreePath()); if ($this->useRealPath()) { $realPath = str_replace($node->getTreeRealPath(), $parent->getTreeRealPath() . $this->separator . $node->{$this->columnAlias}, $node->getTreeRealPath()); } } $fields = [$node->getColumnTreePath() => str_replace('//', '/', $path), $node->getColumnTreePid() => $parent->getKey(), $node->getColumnTreeOrder() => $node->getTreeOrder(), $node->getColumnTreeDepth() => count($this->getExplodedPath($path)) - 1]; if ($this->useRealPath()) { $fields[$node->getColumnTreeRealPath()] = $realPath; } $node->update($fields); foreach ($children as $child) { $path = str_replace($child->getTreePath(), $node->getTreePath() . $node->getKey() . $this->separator, $child->getTreePath()); $fields = [$child->getColumnTreePath() => $path]; if ($this->useRealPath()) { $realPath = str_replace($child->getTreeRealPath(), $node->getTreeRealPath() . $this->separator . $child->{$this->columnAlias}, $child->getTreeRealPath()); $fields[$node->getColumnTreeRealPath()] = $realPath; } $child->update($fields); } return $this; }