Skip to content

Commit c1c280b

Browse files
authored
Merge pull request #67 from alexgiuvara/master
filter out orderBy duplicates
2 parents 3ba87ab + e2d3fcf commit c1c280b

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/QueryBuilder.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected function findFilter(string $property) : ? Filter
222222

223223
protected function addSortsToQuery(Collection $sorts)
224224
{
225-
$sorts
225+
$this->filterDuplicates($sorts)
226226
->each(function (string $sort) {
227227
$descending = $sort[0] === '-';
228228

@@ -232,6 +232,25 @@ protected function addSortsToQuery(Collection $sorts)
232232
});
233233
}
234234

235+
protected function filterDuplicates(Collection $sorts): Collection
236+
{
237+
if (! is_array($orders = $this->getQuery()->orders)) {
238+
return $sorts;
239+
}
240+
241+
return $sorts->reject(function (string $sort) use ($orders) {
242+
$toSort = [
243+
'column' => ltrim($sort, '-'),
244+
'direction' => ($sort[0] === '-') ? 'desc' : 'asc',
245+
];
246+
foreach ($orders as $order) {
247+
if ($order === $toSort) {
248+
return true;
249+
}
250+
}
251+
});
252+
}
253+
235254
protected function addIncludesToQuery(Collection $includes)
236255
{
237256
$includes

tests/SortTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,24 @@ public function setUp()
2727
/** @test */
2828
public function it_can_sort_a_collection_ascending()
2929
{
30+
\DB::enableQueryLog();
3031
$sortedModels = $this
3132
->createQueryFromSortRequest('name')
3233
->get();
3334

35+
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
3436
$this->assertSortedAscending($sortedModels, 'name');
3537
}
3638

3739
/** @test */
3840
public function it_can_sort_a_collection_descending()
3941
{
42+
\DB::enableQueryLog();
4043
$sortedModels = $this
4144
->createQueryFromSortRequest('-name')
4245
->get();
4346

47+
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" desc');
4448
$this->assertSortedDescending($sortedModels, 'name');
4549
}
4650

@@ -89,22 +93,26 @@ public function it_wont_sort_if_no_sort_query_parameter_is_given()
8993
/** @test */
9094
public function it_uses_default_sort_parameter()
9195
{
96+
\DB::enableQueryLog();
9297
$sortedModels = QueryBuilder::for(TestModel::class, new Request())
9398
->allowedSorts('name')
9499
->defaultSort('name')
95100
->get();
96101

102+
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
97103
$this->assertSortedAscending($sortedModels, 'name');
98104
}
99105

100106
/** @test */
101107
public function it_can_allow_multiple_sort_parameters()
102108
{
109+
\DB::enableQueryLog();
103110
$sortedModels = $this
104111
->createQueryFromSortRequest('name')
105112
->allowedSorts('id', 'name')
106113
->get();
107114

115+
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc');
108116
$this->assertSortedAscending($sortedModels, 'name');
109117
}
110118

@@ -123,14 +131,15 @@ public function it_can_allow_multiple_sort_parameters_as_an_array()
123131
public function it_can_sort_by_multiple_columns()
124132
{
125133
factory(TestModel::class, 3)->create(['name' => 'foo']);
134+
\DB::enableQueryLog();
126135

127136
$sortedModels = $this
128137
->createQueryFromSortRequest('name,-id')
129138
->allowedSorts('name', 'id')
130139
->get();
131140

132141
$expected = TestModel::orderBy('name')->orderByDesc('id');
133-
142+
$this->assertSame(\DB::getQueryLog()[0]['query'], 'select "test_models".* from "test_models" order by "name" asc, "id" desc');
134143
$this->assertEquals($expected->pluck('id'), $sortedModels->pluck('id'));
135144
}
136145

0 commit comments

Comments
 (0)