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 460 forks source link

withoutNode(node) does not match descendants #224

Open judgej opened 8 years ago

judgej commented 8 years ago

When using withoutNode(node) to fetch a tree, this method will filter out individual nodes, but will leave the descendants of those nodes in the list, kind of "broken" without parents. I am looking for a way to "prune" a tree from the selected tree, so a whole branch and all descendants are not in that list.

I cannot see a way to do this with the built-in methods, but I could just be missing it. Is that the case?

This is how I am working around this at the moment:

        $tree = Model::where('slug', 'root_slug')->first()
            ->descendantsAndSelf()
            ->orderBy('lft')
            ->where(function($query) use($prune) {
                $query->where('lft', '<', $prune->lft)
                    ->orWhere('lft', '>', $prune->rgt);
            })
            ->get();

Here, $prune is a node that I am editing, and wish to give the user in the admin screen another node to set as a parent for this node. So in this case, the node can be placed anywhere on the tree except on itself, or on any of its current descendants. This gives me that list.

I'm using a root slug here to limit this tree, but you may just take the complete nested set for the whole table - makes no odds. The key is excluding the $prune node and everything under it.

Is this already supported, perhaps in a scope (allowing multiple branches to be pruned in the database selection) or does it need a PR ;-)