src/Database/NestedSet/Builder.php
public function rebuildTree(array $data, $delete = FALSE)
    {
        if ($this->model->usesSoftDelete()) {
            $this->withTrashed();
        }

        $existing = $this->get()->getDictionary();
        $dictionary = [];

        $this->buildRebuildDictionary($dictionary, $data, $existing);

        if (!empty($existing)) {
            if ($delete && !$this->model->usesSoftDelete()) {
                $this->model
                    ->newScopedQuery()
                    ->whereIn($this->model->getKeyName(), array_keys($existing))
                    ->delete();
            }
            else {
                foreach ($existing as $model) {
                    $dictionary[$model->getParentId()][] = $model;

                    if ($delete && $this->model->usesSoftDelete() &&
                        !$model->{$model->getDeletedAtColumn()}
                    ) {
                        $time = $this->model->fromDateTime($this->model->freshTimestamp());

                        $model->{$model->getDeletedAtColumn()} = $time;
                    }
                }
            }
        }

        return $this->fixNodes($dictionary);
    }
public function rebuildTree(array $data, $delete = FALSE)
    {
        if ($this->model->usesSoftDelete()) {
            $this->withTrashed();
        }

        $existing = $this->get()->getDictionary();
        $dictionary = [];

        $this->buildRebuildDictionary($dictionary, $data, $existing);

        if (!empty($existing)) {
            if ($delete && !$this->model->usesSoftDelete()) {
                $this->model
                    ->newScopedQuery()
                    ->whereIn($this->model->getKeyName(), array_keys($existing))
                    ->delete();
            }
            else {
                foreach ($existing as $model) {
                    $dictionary[$model->getParentId()][] = $model;

                    if ($delete && $this->model->usesSoftDelete() &&
                        !$model->{$model->getDeletedAtColumn()}
                    ) {
                        $time = $this->model->fromDateTime($this->model->freshTimestamp());

                        $model->{$model->getDeletedAtColumn()} = $time;
                    }
                }
            }
        }

        return $this->fixNodes($dictionary);
    }
Rebuild the tree based on raw data.

If item data does not contain primary key, new node will be created.

@param array $data
@param bool $delete Whether to delete nodes that exists but not in the data
array

@return int