laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

Tree between 2 nodes

Open okrimsoft opened this issue 4 years ago • 4 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

okrimsoft avatar Sep 06 '21 17:09 okrimsoft

This is described in the readme: https://github.com/lazychaser/laravel-nestedset#getting-a-subtree

David-Petrov avatar Sep 07 '21 10:09 David-Petrov

hi @David-Petrov yes, you have right, i placed a wrong question. Just corrected. Thanks

okrimsoft avatar Sep 07 '21 10:09 okrimsoft

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

David-Petrov avatar Sep 07 '21 10:09 David-Petrov

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

okrimsoft avatar Sep 07 '21 11:09 okrimsoft