Skip to content

Commit c2611eb

Browse files
authored
Merge pull request #1 from sohrabafard/sohrabafard-patch-1
Add Callback For Create method
2 parents 351fab2 + 3e05c73 commit c2611eb

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/NodeTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,9 @@ public function newCollection(array $models = array())
749749
* Use `children` key on `$attributes` to create child nodes.
750750
*
751751
* @param self $parent
752+
* @param callable $callback
752753
*/
753-
public static function create(array $attributes = [], self $parent = null)
754+
public static function create(array $attributes = [], self $parent = null, callable $callbackForEachChild = null)
754755
{
755756
$children = array_pull($attributes, 'children');
756757

@@ -766,9 +767,12 @@ public static function create(array $attributes = [], self $parent = null)
766767
$relation = new EloquentCollection;
767768

768769
foreach ((array)$children as $child) {
769-
$relation->add($child = static::create($child, $instance));
770+
$relation->add($child = static::create($child, $instance, $callbackForEachChild));
770771

771772
$child->setRelation('parent', $instance);
773+
774+
if(isset($callback))
775+
call_user_func($callbackForEachChild, $child);
772776
}
773777

774778
$instance->refreshNode();

0 commit comments

Comments
 (0)