Skip to content

Commit e38b022

Browse files
marcoocramAlexVanderbist
authored andcommitted
Adds chunk method to query builder to parse sorts (spatie#299)
This PR adds the chunk function to ensure parse sorts before enforceOrderBy method is executed on chunk queries.
1 parent a0f5902 commit e38b022

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/QueryBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
101101
return parent::simplePaginate($perPage, $columns, $pageName, $page);
102102
}
103103

104+
public function chunk($count, callable $callback)
105+
{
106+
$this->parseSorts();
107+
108+
return parent::chunk($count, $callback);
109+
}
110+
104111
/**
105112
* Add the model, scopes, eager loaded relationships, local macro's and onDelete callback
106113
* from the $builder to this query builder.

tests/SortTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ public function it_can_sort_a_query_with_custom_select()
109109
$this->assertQueryExecuted('select "id", "name" from "test_models" order by "id" desc limit 15 offset 0');
110110
}
111111

112+
/** @test */
113+
public function it_can_sort_a_chunk_query()
114+
{
115+
$this
116+
->createQueryFromSortRequest('-name')
117+
->chunk(100, function ($models) {
118+
//
119+
});
120+
121+
$this->assertQueryExecuted('select * from "test_models" order by "name" desc limit 100 offset 0');
122+
}
123+
112124
/** @test */
113125
public function it_can_guard_against_sorts_that_are_not_allowed()
114126
{

0 commit comments

Comments
 (0)