/** * The "booting" method of the model. * * @return void */ protected static function boot() { parent::boot(); static::updating(function ($navItem) { $dirty = $navItem->getDirty(); $oldNavItem = self::where('id', '=', $navItem->id)->first(); $oldParent = $oldNavItem->parent; $oldParentId = $oldParent->id; if (isset($dirty[$navItem->getParentColumnName()]) && $dirty[$navItem->getParentColumnName()] == $oldParentId) { unset($navItem->{$navItem->getParentColumnName()}); static::$moveToNewParentId = FALSE; } }); }
/** * Reset parent ID. * * Removes the parent_id field from the model's attributes and sets $moveToNewParentId * static property on the parent Baum\Node model class to false to prevent Baum from * triggering a move. This can be required because Baum triggers a parent move, which * puts the item last in the list, even if the old and new parents are the same. * * @return void */ protected function stopBaumParentMove() { unset($this->{$this->getParentColumnName()}); static::$moveToNewParentId = false; }
/** * Store the parent_id if the attribute is modified so as we are able to move * the node to this new parent after saving. * * @return void */ public function storeNewParent() { if ($this->isDirty($this->getParentColumnName()) && ($this->exists || !$this->isRoot())) { static::$moveToNewParentId = $this->getParentId(); } else { static::$moveToNewParentId = false; } }