From bd24947fd1fc49d37354cd1195b4e5756e5560d4 Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Tue, 27 Nov 2018 21:47:12 +0000 Subject: [PATCH 1/3] Fixes ordering by a relationship. --- src/QueryDataTable.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 1ac591df..0b1c7128 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -617,6 +617,13 @@ protected function defaultOrdering() }) ->each(function ($orderable) { $column = $this->resolveRelationColumn($orderable['name']); + + if (str_contains($column, '.')) { + $this->query->addSelect([ + $this->query->getModel()->getTable() . '.*', + $column + ]); + } if ($this->hasOrderColumn($column)) { $this->applyOrderColumn($column, $orderable); From cf570090ccb30f29e833bc8d2c734171fd104876 Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Tue, 27 Nov 2018 22:35:42 +0000 Subject: [PATCH 2/3] Added a check for duplicate selects. --- src/QueryDataTable.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 0b1c7128..3d5ed941 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -619,10 +619,7 @@ protected function defaultOrdering() $column = $this->resolveRelationColumn($orderable['name']); if (str_contains($column, '.')) { - $this->query->addSelect([ - $this->query->getModel()->getTable() . '.*', - $column - ]); + $this->applySelects([$column]); } if ($this->hasOrderColumn($column)) { @@ -635,6 +632,29 @@ protected function defaultOrdering() } }); } + + /** + * Apply selects to query. + * + * @param array $selects + */ + public function applySelects(array $selects) + { + $selects = array_merge( + [$this->query->getModel()->getTable() . '.*'], + $selects + ); + + $columns = $this->query->getQuery()->columns ?? []; + + $this->query->addSelect( + collect($selects) + ->unique() + ->reject(function ($select) use ($columns) { + return in_array($select, $columns); + })->toArray() + ); + } /** * Check if column has custom sort handler. From 9a2ceb7c2409c7de9b1899431ceaf8343dc208de Mon Sep 17 00:00:00 2001 From: Nathan Ward Date: Tue, 27 Nov 2018 23:17:36 +0000 Subject: [PATCH 3/3] Fixed styling issues. --- src/QueryDataTable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 3d5ed941..5a22dc50 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -617,7 +617,7 @@ protected function defaultOrdering() }) ->each(function ($orderable) { $column = $this->resolveRelationColumn($orderable['name']); - + if (str_contains($column, '.')) { $this->applySelects([$column]); } @@ -632,7 +632,7 @@ protected function defaultOrdering() } }); } - + /** * Apply selects to query. *