Quellcode durchsuchen

Grid\Model::orderBy 支持关联关系字段排序

jqh vor 5 Jahren
Ursprung
Commit
4bab9cb76a

+ 2 - 2
src/Grid/Concerns/HasTree.php

@@ -158,8 +158,8 @@ HTML
     {
         if (
             $sortable
-            && ! $this->findQueryByMethod('orderBy')
-            && ! $this->findQueryByMethod('orderByDesc')
+            && $this->findQueryByMethod('orderBy')->isEmpty()
+            && $this->findQueryByMethod('orderByDesc')->isEmpty()
             && ($orderColumn = $this->repository->getOrderColumn())
         ) {
             $this->orderBy($orderColumn)

+ 2 - 4
src/Grid/Model.php

@@ -510,13 +510,11 @@ class Model
      *
      * @param $method
      *
-     * @return static
+     * @return Collection
      */
     public function findQueryByMethod($method)
     {
-        return $this->queries->first(function ($query) use ($method) {
-            return $query['method'] == $method;
-        });
+        return $this->queries->where('method', $method);
     }
 
     /**

+ 26 - 1
src/Repositories/EloquentRepository.php

@@ -188,11 +188,36 @@ class EloquentRepository extends Repository implements TreeRepository
         [$column, $type, $cast] = $model->getSort();
 
         if (empty($column) || empty($type)) {
+            $orders = $model->findQueryByMethod('orderBy')->merge($model->findQueryByMethod('orderByDesc'));
+
+            $model->resetOrderBy();
+
+            $orders->each(function ($orderBy) use ($model) {
+                $column = $orderBy['arguments'][0];
+                $type = $orderBy['method'] === 'orderByDesc' ? 'desc' : ($orderBy['arguments'][1] ?? 'asc');
+                $cast = null;
+
+                $this->addOrderBy($model, $column, $type, $cast);
+            });
+
             return;
         }
 
         $model->resetOrderBy();
 
+        $this->addOrderBy($model, $column, $type, $cast);
+    }
+
+    /**
+     * @param Grid\Model $model
+     * @param string $column
+     * @param string $type
+     * @param string $cast
+     *
+     * @throws \Exception
+     */
+    protected function addOrderBy(Grid\Model $model, $column, $type, $cast)
+    {
         $explodedCols = explode('.', $column);
         $isRelation = empty($explodedCols[1]) ? false : method_exists($this->model(), $explodedCols[0]);
 
@@ -345,7 +370,7 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     protected function setPaginate(Grid\Model $model)
     {
-        $paginate = $model->findQueryByMethod('paginate');
+        $paginate = $model->findQueryByMethod('paginate')->first();
 
         $model->rejectQuery(['paginate']);
 

+ 1 - 1
src/Repositories/QueryBuilderRepository.php

@@ -198,7 +198,7 @@ class QueryBuilderRepository extends Repository implements TreeRepository
      */
     protected function setPaginate(Grid\Model $model)
     {
-        $paginate = $model->findQueryByMethod('paginate');
+        $paginate = $model->findQueryByMethod('paginate')->first();
 
         $model->rejectQuery(['paginate']);