Group descendants by position #190
-
Hi everyone, posting this discussion to seek help. I'm trying to efficiently retrieve the descendants of the current logged-in user. While I have managed to fetch the descendants using the following code:
The query above returns data as follows:
I'm looking to group these descendants by their position (left and right), but I want to start the grouping with bill - left and eddie76 - right, as these two accounts are positioned as the left and right children of the logged-in user ( The desired outcome should be as follows:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi @whoami15, |
Beta Was this translation helpful? Give feedback.
-
Hi @staudenmeir Yeah, I added that but I'm getting an error.
Error
So what I did was add it after the
The problem with this is that But what I want is to show her in the I guess I need to get the Level 1 left and right of the root account first. |
Beta Was this translation helpful? Give feedback.
-
Now I see the issue. You can group them by the first segment of their $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'
); |
Beta Was this translation helpful? Give feedback.
Now I see the issue.
You can group them by the first segment of their
path
: