Skip to content

Commit 30becf2

Browse files
committed
Apply default sort order column and direction
1 parent d03b95c commit 30becf2

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Traits/WithSorting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ trait WithSorting
1515
public array $sorts = [];
1616
public array $sortNames = [];
1717
public array $sortDirectionNames = [];
18+
public string $defaultSortColumn = '';
19+
public string $defaultSortDirection = 'asc';
1820

1921
public function sortBy(string $field): ?string
2022
{
@@ -42,6 +44,10 @@ public function sortBy(string $field): ?string
4244
*/
4345
public function applySorting($query)
4446
{
47+
if (! empty($this->defaultSortColumn) && empty($this->sorts)) {
48+
return $query->orderBy($this->defaultSortColumn, $this->defaultSortDirection);
49+
}
50+
4551
foreach ($this->sorts as $field => $direction) {
4652
if (optional($this->getColumn($field))->hasSortCallback()) {
4753
$query = app()->call($this->getColumn($field)->getSortCallback(), ['query' => $query, 'direction' => $direction]);

0 commit comments

Comments
 (0)