Skip to content

Group descendants by position #190

Answered by staudenmeir
whoami15 asked this question in Q&A
Discussion options

You must be logged in to vote

Now I see the issue.

You can group them by the first segment of their path:

$descendants = Account::find($accountId)
    ->descendants()
    ->depthFirst()
    ->get();

$leftChild = $descendants->where('position', 'left')->where('depth', 1)->first();

$groupedDescendants = $descendants->groupBy(
    fn($item) => explode('.', $item->path)[0] == $leftChild->id ? 'left' : 'right'
);

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@whoami15
Comment options

Answer selected by whoami15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants