public function updating(\Illuminate\Database\Eloquent\Model $model) { // An node is being updated. // Rebuild the tree if the parent was changed. // Get column names. // Missing required columns will throw an exception. $pkColumn = $model->getKeyName(); $leftColumn = $model->getLeftColumn(); $rightColumn = $model->getRightColumn(); $parentColumn = $model->getParentColumn(); $depthColumn = $model->getDepthColumn(); $groupColumn = $model->getGroupColumn(); $originalParentId = $model->getOriginal($parentColumn); if ($originalParentId !== $model->{$parentColumn}) { $model->syncOriginalAttribute($parentColumn); echo 'parent changed from "' . $originalParentId . '" to "' . $model->{$parentColumn} . '"'; if (!is_null($originalParentId)) { // Load original parent $originalParent = $model::findOrFail($originalParentId, array_filter([$pkColumn, $leftColumn, $rightColumn, $depthColumn, $groupColumn])); } if (!is_null($model->{$parentColumn})) { // Load new parent $newParent = $model::findOrFail($model->{$parentColumn}, array_filter([$pkColumn, $leftColumn, $rightColumn, $depthColumn, $groupColumn])); if ($newParent->{$leftColumn} > $model->{$leftColumn} and $newParent->{$leftColumn} < $model->{$rightColumn}) { // This change would create a circular tree. throw new \Exception('Changing this node\'s parent to ' . $model->{$parentColumn} . ' creates a circular reference'); } } //$originalParent = $model->where // @TODO: Move this to the Trait. Override save(). dd($modelId); /* SELECT id FROM node WHERE node.left BETWEEN 1 AND 10 AND node.id = 8 */ // Rebuild the entire tree. // @TODO: Be smarter about this method. dd($model); $rootNode = $model::where($parentColumn, '=', null)->first(); $rootNode->buildTree(); } return true; }