From 3e05c736c1fb1bfb7935ea2f682fa990092c69ac Mon Sep 17 00:00:00 2001 From: sohrabafard Date: Mon, 29 Apr 2019 13:27:09 +0430 Subject: [PATCH] Add Callback For Create method ``` public static function create(array $attributes = [], self $parent = null, callable $callbackForEachChild = null) { .... } ``` --- src/NodeTrait.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/NodeTrait.php b/src/NodeTrait.php index 167c001..313a144 100644 --- a/src/NodeTrait.php +++ b/src/NodeTrait.php @@ -749,8 +749,9 @@ public function newCollection(array $models = array()) * Use `children` key on `$attributes` to create child nodes. * * @param self $parent + * @param callable $callback */ - public static function create(array $attributes = [], self $parent = null) + public static function create(array $attributes = [], self $parent = null, callable $callbackForEachChild = null) { $children = array_pull($attributes, 'children'); @@ -766,9 +767,12 @@ public static function create(array $attributes = [], self $parent = null) $relation = new EloquentCollection; foreach ((array)$children as $child) { - $relation->add($child = static::create($child, $instance)); + $relation->add($child = static::create($child, $instance, $callbackForEachChild)); $child->setRelation('parent', $instance); + + if(isset($callback)) + call_user_func($callbackForEachChild, $child); } $instance->refreshNode();