Example #1
0
 /**
  * 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
  */
 public function rebuildTree(array $data, $delete = false)
 {
     $existing = $this->get()->getDictionary();
     $dictionary = [];
     $this->buildRebuildDictionary($dictionary, $data, $existing);
     if (!empty($existing)) {
         if ($delete) {
             $this->model->newScopedQuery()->whereIn($this->model->getKeyName(), array_keys($existing))->forceDelete();
         } else {
             foreach ($existing as $model) {
                 $dictionary[$model->getParentId()][] = $model;
             }
         }
     }
     return $this->fixNodes($dictionary);
 }