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();