@@ -211,25 +211,26 @@ private function buildTreeFromResults($results, $parent = null): Collection
211211
212212 // Group results by their parent IDs
213213 foreach ($ results as $ result ) {
214- // Cache the result ID as seen
215- $ resultCache [$ result ->id ]['in_set ' ] = 1 ;
214+ // Cache the result as seen
215+ $ resultKey = $ this ->getCustomKey ($ result );
216+ $ resultCache [$ resultKey ]['in_set ' ] = 1 ;
216217 // Move any cached children to the result map
217- if (isset ($ resultCache [$ result -> id ]['children ' ])){
218- $ resultMap [$ result -> id ] = array_merge ($ resultMap [$ result -> id ], $ resultCache [$ result -> id ]['children ' ]);
219- unset($ resultCache [$ result -> id ]['children ' ]);
218+ if (isset ($ resultCache [$ resultKey ]['children ' ])){
219+ $ resultMap [$ resultKey ] = array_merge ($ resultMap [$ resultKey ], $ resultCache [$ resultKey ]['children ' ]);
220+ unset($ resultCache [$ resultKey ]['children ' ]);
220221 }
221- $ parentId = $ result ->{$ this ->getParentAttribute ()};
222- if (! isset ($ resultCache [$ parentId ])) {
222+ $ parentKey = $ result ->{$ this ->getParentAttribute ()};
223+ if (! isset ($ resultCache [$ parentKey ])) {
223224 // Before adding results to the map, cache the parentId to hold until the parent is confirmed to be in the result set
224- $ resultCache [$ parentId ]['in_set ' ] = 0 ;
225- $ resultCache [$ parentId ]['children ' ] = [];
225+ $ resultCache [$ parentKey ]['in_set ' ] = 0 ;
226+ $ resultCache [$ parentKey ]['children ' ] = [];
226227 }
227- if ($ resultCache [$ parentId ]['in_set ' ]){
228+ if ($ resultCache [$ parentKey ]['in_set ' ]){
228229 // if the parent has been confirmed to be in the set, add directly to result map
229- $ resultMap [$ parentId ][] = $ result ;
230+ $ resultMap [$ parentKey ][] = $ result ;
230231 } else {
231232 // otherwise, hold the result in the children cache until the parent is confirmed to be in the result set
232- $ resultCache [$ parentId ]['children ' ][] = $ result ;
233+ $ resultCache [$ parentKey ]['children ' ][] = $ result ;
233234 }
234235 }
235236
0 commit comments