Skip to content

Commit a05ce6d

Browse files
[11.x] Paginator total override (#46410)
* allow override of the Builder `paginate()` total this allows the user to set the total number of results a query returns. If the user provides this argument, the `paginate()` method will skip running its query to determine the total row count. * use value helper --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 68caccf commit a05ce6d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Illuminate/Database/Eloquent/Builder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,16 @@ public function pluck($column, $key = null)
889889
* @param array|string $columns
890890
* @param string $pageName
891891
* @param int|null $page
892+
* @param \Closure|int|null $total
892893
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
893894
*
894895
* @throws \InvalidArgumentException
895896
*/
896-
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
897+
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
897898
{
898899
$page = $page ?: Paginator::resolveCurrentPage($pageName);
899900

900-
$total = $this->toBase()->getCountForPagination();
901+
$total = value($total) ?? $this->toBase()->getCountForPagination();
901902

902903
$perPage = ($perPage instanceof Closure
903904
? $perPage($total)

src/Illuminate/Database/Query/Builder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2727,13 +2727,14 @@ protected function runSelect()
27272727
* @param array|string $columns
27282728
* @param string $pageName
27292729
* @param int|null $page
2730+
* @param \Closure|int|null $total
27302731
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
27312732
*/
2732-
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
2733+
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
27332734
{
27342735
$page = $page ?: Paginator::resolveCurrentPage($pageName);
27352736

2736-
$total = $this->getCountForPagination();
2737+
$total = value($total) ?? $this->getCountForPagination();
27372738

27382739
$perPage = $perPage instanceof Closure ? $perPage($total) : $perPage;
27392740

tests/Database/DatabaseQueryBuilderTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -4450,6 +4450,30 @@ public function testPaginateWithSpecificColumns()
44504450
]), $result);
44514451
}
44524452

4453+
public function testPaginateWithTotalOverride()
4454+
{
4455+
$perPage = 16;
4456+
$columns = ['id', 'name'];
4457+
$pageName = 'page-name';
4458+
$page = 1;
4459+
$builder = $this->getMockQueryBuilder();
4460+
$path = 'http://foo.bar?page=3';
4461+
4462+
$results = collect([['id' => 3, 'name' => 'Taylor'], ['id' => 5, 'name' => 'Mohamed']]);
4463+
4464+
$builder->shouldReceive('getCountForPagination')->never();
4465+
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
4466+
$builder->shouldReceive('get')->once()->andReturn($results);
4467+
4468+
Paginator::currentPathResolver(function () use ($path) {
4469+
return $path;
4470+
});
4471+
4472+
$result = $builder->paginate($perPage, $columns, $pageName, $page, 10);
4473+
4474+
$this->assertEquals(10, $result->total());
4475+
}
4476+
44534477
public function testCursorPaginate()
44544478
{
44554479
$perPage = 16;

0 commit comments

Comments
 (0)