| Conditions | 3 | 
| Paths | 3 | 
| Total Lines | 30 | 
| Code Lines | 15 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Tests | 17 | 
| CRAP Score | 3 | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
| 1 | <?php | ||
| 21 | 18 | public function toTree($childrenRelation = 'children') | |
| 22 |     { | ||
| 23 | 18 |         if ($this->isEmpty()) { | |
| 24 | 6 | return $this; | |
| 25 | } | ||
| 26 | |||
| 27 | 12 | $model = $this->first(); | |
| 28 | 12 | ||
| 29 | 12 | $parentKeyName = $model->getParentKeyName(); | |
| 30 | |||
| 31 | 12 | $localKeyName = $model->getLocalKeyName(); | |
| 32 | |||
| 33 | 12 | $depthName = $model->getDepthName(); | |
| 34 | 12 | ||
| 35 | 12 | $depths = $this->pluck($depthName); | |
| 36 | |||
| 37 | 12 | $tree = new static( | |
| 38 | $this->where($depthName, $depths->min())->values() | ||
| 39 | 12 | ); | |
| 40 | 12 | ||
| 41 | 12 | $itemsByParentKey = $this->groupBy($parentKeyName); | |
| 42 | 12 | ||
| 43 | 12 |         foreach ($this->items as $item) { | |
| 44 | $item->setRelation( | ||
| 45 | $childrenRelation, | ||
| 46 | 12 | $itemsByParentKey[$item->$localKeyName] ?? new static() | |
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | return $tree; | ||
| 51 | } | ||
| 53 |