1: <?php
2:
3: namespace Baril\Bonsai\Concerns;
4:
5: /**
6: * @deprecated Instead, use Orderable or Ordered trait together with BelongsToTree.
7: */
8: trait BelongsToOrderedTree
9: {
10: use BelongsToTree {
11: children as _children;
12: descendants as _descendants;
13: getTree as _getTree;
14: }
15: use Orderable;
16:
17: /**
18: * @return \Illuminate\Database\Eloquent\Relation\HasMany
19: */
20: public function children()
21: {
22: return $this->_children()->ordered();
23: }
24:
25: /**
26: * Many-to-many relation to the descendants through the closure table.
27: *
28: * @return \Baril\Bonsai\Relations\BelongsToManyThroughClosures<static::class, $this, \Illuminate\Database\Eloquent\Relations\Pivot>
29: */
30: public function descendants()
31: {
32: return $this->_descendants()
33: ->closes('children', function ($models, $results) {
34: return [$models, $results->sortBy($this->getOrderColumn())];
35: });
36: }
37:
38: /**
39: * @param int|null $depth
40: * @return \Illuminate\Database\Eloquent\Collection
41: */
42: public static function getTree($depth = null)
43: {
44: return static::_getTree($depth)
45: // Sort roots (the rest is already sorted):
46: ->sortBy((new static())->getOrderColumn())
47: ->values();
48: }
49: }
50: