diff --git a/src/ConditionsGeneratorTrait.php b/src/ConditionsGeneratorTrait.php index 498933b..6721ec7 100644 --- a/src/ConditionsGeneratorTrait.php +++ b/src/ConditionsGeneratorTrait.php @@ -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) { diff --git a/tests/Unit/ConditionGeneratorTrait/HandlePaginate.php b/tests/Unit/ConditionGeneratorTrait/HandlePaginate.php index 712c5a7..e5a5ed8 100644 --- a/tests/Unit/ConditionGeneratorTrait/HandlePaginate.php +++ b/tests/Unit/ConditionGeneratorTrait/HandlePaginate.php @@ -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();