Skip to content

Commit

Permalink
Fix default order bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhichao-poper committed Jul 16, 2020
1 parent 11ef4d4 commit b43f069
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ConditionsGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ protected function handlePaginate()
continue;
}
$field = substr($sort, 1);
$orders[$field] = !array_key_exists($field, $orders) && strpos($sort, '+') === 0 ? 'asc' : 'desc';
if (! array_key_exists($field, $orders)) {
$orders[$field] = strpos($sort, '+') === 0 ? 'asc' : 'desc';
}
}

if ($sort instanceof Expression) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/ConditionGeneratorTrait/HandlePaginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ public function test_sorts()
], Arr::get($conditions, 'order'));
}

public function test_over_default_sort()
{
$fakeRequest = new FakeRequest();
$conditions = $fakeRequest->getConditions([
'paginator' => [
'sort' => '-id',
],
]);

$this->assertEquals([
'id' => 'desc',
], Arr::get($conditions, 'order'));
}

public function test_order()
{
$fakeRequest = new FakeRequest();
Expand Down

0 comments on commit b43f069

Please sign in to comment.