ModelTree.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Support\Arr;
  5. /**
  6. * 嵌套结构模型辅助 trait
  7. *
  8. * Trait ModelTree
  9. * @package App\Traits
  10. */
  11. trait ModelTree
  12. {
  13. /**
  14. * 要排除的节点 id,子元素都会被排除
  15. *
  16. * @var int
  17. */
  18. protected $except = 0;
  19. protected static $branchOrder = [];
  20. protected function parentColumn()
  21. {
  22. return 'parent_id';
  23. }
  24. protected function orderColumn()
  25. {
  26. return 'order';
  27. }
  28. protected function idColumn()
  29. {
  30. return 'id';
  31. }
  32. /**
  33. * 排除指定的 id,排除后,该 id 和其子孙记录,都会排除
  34. *
  35. * @param int $id
  36. *
  37. * @return $this
  38. */
  39. public function treeExcept(int $id)
  40. {
  41. $this->except = $id;
  42. return $this;
  43. }
  44. /**
  45. * 构建嵌套数组
  46. *
  47. * @return array
  48. */
  49. public function toTree(): array
  50. {
  51. return $this->buildNestedArray();
  52. }
  53. /**
  54. * 构建嵌套数组
  55. *
  56. * @param array $nodes
  57. * @param int $parentId
  58. *
  59. * @return array
  60. */
  61. protected function buildNestedArray(array $nodes = [], $parentId = 0): array
  62. {
  63. $branch = [];
  64. if (empty($nodes)) {
  65. $nodes = $this->allNodes();
  66. }
  67. static $parentIds;
  68. $parentIds = $parentIds ?: array_flip(array_column($nodes, $this->parentColumn()));
  69. foreach ($nodes as $node) {
  70. if ($this->ignoreTreeNode($node)) {
  71. continue;
  72. }
  73. if ($node[$this->parentColumn()] == $parentId) {
  74. $children = $this->buildNestedArray($nodes, $node[$this->idColumn()]);
  75. // 没有子菜单也显示一个空的数组,避免前端没有 children 时,不能响应式
  76. $node['children'] = $children;
  77. $branch[] = $node;
  78. }
  79. }
  80. return $branch;
  81. }
  82. /**
  83. * 按排序查出所有记录
  84. *
  85. * @return array
  86. */
  87. protected function allNodes(): array
  88. {
  89. return $this->allNodesQuery()->get()->toArray();
  90. }
  91. /**
  92. * @return Builder|mixed
  93. */
  94. protected function allNodesQuery(): Builder
  95. {
  96. return static::query()
  97. ->when($this->except, function (Builder $query) {
  98. $query->where($this->idColumn(), '<>', $this->except)
  99. ->where($this->parentColumn(), '<>', $this->except);
  100. })
  101. ->orderBy($this->orderColumn());
  102. }
  103. public function children()
  104. {
  105. return $this->hasMany(static::class, $this->parentColumn(), $this->idColumn());
  106. }
  107. public function parent()
  108. {
  109. return $this->belongsTo(static::class, $this->parentColumn(), $this->idColumn());
  110. }
  111. public function delete()
  112. {
  113. $this->children->each->delete();
  114. return parent::delete();
  115. }
  116. /**
  117. * 是否跳过节点的处理
  118. *
  119. * @param array $node 当前节点
  120. *
  121. * @return bool
  122. */
  123. protected function ignoreTreeNode(array $node): bool
  124. {
  125. return false;
  126. }
  127. protected function setBranchOrder(array $order)
  128. {
  129. static::$branchOrder = array_flip(Arr::flatten($order));
  130. static::$branchOrder = array_map(function ($item) {
  131. return ++$item;
  132. }, static::$branchOrder);
  133. }
  134. public function saveOrder($tree = [], $parentId = 0)
  135. {
  136. if (empty(static::$branchOrder)) {
  137. $this->setBranchOrder($tree);
  138. }
  139. foreach ($tree as $branch) {
  140. /** @var ModelTree $node */
  141. $node = static::find($branch[$this->idColumn()]);
  142. $node->{$node->parentColumn()} = $parentId;
  143. $node->{$node->orderColumn()} = static::$branchOrder[$branch[$this->idColumn()]];
  144. $node->save();
  145. if (isset($branch['children'])) {
  146. static::saveOrder($branch['children'], $branch[$this->idColumn()]);
  147. }
  148. }
  149. }
  150. }