jqh 5 年之前
父节点
当前提交
0f56cf292d
共有 3 个文件被更改,包括 32 次插入17 次删除
  1. 4 7
      src/Models/Menu.php
  2. 6 0
      src/Repositories/EloquentRepository.php
  3. 22 10
      src/Traits/ModelTree.php

+ 4 - 7
src/Models/Menu.php

@@ -18,8 +18,8 @@ class Menu extends Model
 {
     use MenuCache,
         ModelTree {
-            ModelTree::boot as treeBoot;
-        }
+        ModelTree::boot as treeBoot;
+    }
 
     /**
      * The attributes that are mass assignable.
@@ -74,7 +74,7 @@ class Menu extends Model
      */
     public function allNodes(bool $force = false): array
     {
-        if ($force || $this->queryCallback instanceof \Closure) {
+        if ($force || $this->queryCallbacks) {
             return $this->fetchAll();
         }
 
@@ -93,10 +93,7 @@ class Menu extends Model
 
         $byOrder = 'ROOT ASC, '.$orderColumn;
 
-        $self = new static();
-        if ($this->queryCallback instanceof \Closure) {
-            $self = call_user_func($this->queryCallback, $self);
-        }
+        $self = $this->callQueryCallbacks(new static());
 
         if (static::withPermission()) {
             $self = $self->with('permissions');

+ 6 - 0
src/Repositories/EloquentRepository.php

@@ -451,6 +451,12 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     public function toTree()
     {
+        if ($this->relations) {
+            $this->withQuery(function ($model) {
+                return $model->with($this->relations);
+            });
+        }
+
         return $this->eloquent()->toTree();
     }
 

+ 22 - 10
src/Traits/ModelTree.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Traits;
 
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Tree;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\DB;
@@ -22,9 +23,9 @@ trait ModelTree
     protected static $branchOrder = [];
 
     /**
-     * @var \Closure
+     * @var \Closure[]
      */
-    protected $queryCallback;
+    protected $queryCallbacks = [];
 
     /**
      * Get children of current node.
@@ -113,7 +114,7 @@ trait ModelTree
      */
     public function withQuery(\Closure $query = null)
     {
-        $this->queryCallback = $query;
+        $this->queryCallbacks[] = $query;
 
         return $this;
     }
@@ -147,18 +148,29 @@ trait ModelTree
         $orderColumn = DB::getQueryGrammar()->wrap($this->getOrderColumn());
         $byOrder = 'ROOT ASC, '.$orderColumn;
 
-        $self = new static();
-
-        if ($this->queryCallback instanceof \Closure) {
-            $self = call_user_func($this->queryCallback, $self);
-        }
-
-        return $self->selectRaw('*, '.$orderColumn.' ROOT')
+        return $this->callQueryCallbacks(new static())
+            ->selectRaw('*, '.$orderColumn.' ROOT')
             ->orderByRaw($byOrder)
             ->get()
             ->toArray();
     }
 
+    /**
+     * @param $this $model
+     *
+     * @return $this|Builder
+     */
+    protected function callQueryCallbacks($model)
+    {
+        foreach ($this->queryCallbacks as $callback) {
+            if ($callback) {
+                $model = $callback($model);
+            }
+        }
+
+        return $model;
+    }
+
     /**
      * Set the order of branches in the tree.
      *