diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 1ac591df..5a22dc50 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -618,6 +618,10 @@ protected function defaultOrdering() ->each(function ($orderable) { $column = $this->resolveRelationColumn($orderable['name']); + if (str_contains($column, '.')) { + $this->applySelects([$column]); + } + if ($this->hasOrderColumn($column)) { $this->applyOrderColumn($column, $orderable); } else { @@ -629,6 +633,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. *