Skip to content

Commit

Permalink
Apply default sort order column and direction
Browse files Browse the repository at this point in the history
  • Loading branch information
simple-hacker committed Jun 3, 2021
1 parent d03b95c commit 30becf2
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Traits/WithSorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ trait WithSorting
public array $sorts = [];
public array $sortNames = [];
public array $sortDirectionNames = [];
public string $defaultSortColumn = '';
public string $defaultSortDirection = 'asc';

public function sortBy(string $field): ?string
{
Expand Down Expand Up @@ -42,6 +44,10 @@ public function sortBy(string $field): ?string
*/
public function applySorting($query)
{
if (! empty($this->defaultSortColumn) && empty($this->sorts)) {
return $query->orderBy($this->defaultSortColumn, $this->defaultSortDirection);
}

foreach ($this->sorts as $field => $direction) {
if (optional($this->getColumn($field))->hasSortCallback()) {
$query = app()->call($this->getColumn($field)->getSortCallback(), ['query' => $query, 'direction' => $direction]);
Expand Down

0 comments on commit 30becf2

Please sign in to comment.