baum icon indicating copy to clipboard operation
baum copied to clipboard

Using makeChildOf() in loops doesn't work as expected

Open johnRivs opened this issue 9 years ago • 0 comments

What I have:

$child_1 = factory(User::class)->states('active')->create();
    $child_1_1 = factory(User::class)->states('active')->create()->makeChildOf($child_1);
        $child_1_1_1 = factory(User::class)->states('active')->create()->makeChildOf($child_1_1);
        $child_1_1_2 = factory(User::class)->states('active')->create()->makeChildOf($child_1_1);
        $child_1_1_3 = factory(User::class)->states('active')->create()->makeChildOf($child_1_1);
        $child_1_1_4 = factory(User::class)->create()->makeChildOf($child_1_1);

$child_1_1->cap();

When I do:

public function cap()
{
    $actives = $this->children()
                ->whereActive(true)
                ->limit(2)
                ->get();

    $actives->each(function($active) {
        $active->makeChildOf($this->parent);
    });
}

Only the first $active ends up being a child of $this->parent (which is $child_1).

When I dump $child_1->children after that, I only see $child_1_1 and $child_1_1_1, instead of $child_1_1, $child_1_1_1 and $child_1_1_2.

PS: It'd be even better if we could could have something like $actives->makeChildrenOf($user).

johnRivs avatar Nov 24 '16 03:11 johnRivs