Tree between 2 nodes
okrimsoft opened this issue · comments
Hi, is possible in some way get the subtree between 2 nodes?
With helper i'm able to check if a node isDescendantOf
or isAncestorOf
, but how to get just this part of tree?
Imagine this tree
(ID: 1) - Root
(ID: 2)-- Child 1
(ID: 3)--- Sub child 1
(ID: 4)---- Sub sub child 1
(ID: 5)---- Sub sub child 2
(ID: 6)----- Sub sub sub child 1
(ID: 7)----- Leaf 1
(ID: 8)-- Child 2
For example: how can i do for get only the tree between (ID: 2) Child 1
and (ID: 5) Sub sub child 2
?
Thanks alot for help
This is described in the readme:
https://github.com/lazychaser/laravel-nestedset#getting-a-subtree
hi @David-Petrov yes, you have right, i placed a wrong question. Just corrected.
Thanks
@ExaMuff You should keep in mind that this isn't really an issue with the package, but here's a suggestion: you can query your results withDepth
and then perform a where clause based on the depth
attribute (which gets subselected in the query). Moreover, if you don't really have a problem with loading a bit more models, you can much more easily do that using laravel collection methods to filter the nodes based on their depth in the ram.
Hi @David-Petrov, yes I know is not an issue, but yes can be a suggestion for next release.
Ok i'll try to find a way, for now i think i'll get $subtree = ancestorsAndSelf($end_node)
, then cycle $subtree
until $node->id != $start_node