etrepat / baum

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.
http://etrepat.com/baum
MIT License
2.24k stars 459 forks source link

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

Open johnRivs opened 7 years ago

johnRivs commented 7 years ago

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).