Skip to content

Commit

Permalink
[10.x] allow override of the Builder paginate() total (#46336)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
browner12 and taylorotwell authored Mar 6, 2023
1 parent ee9487f commit df0135b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,15 +880,16 @@ public function pluck($column, $key = null)
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = $this->toBase()->getCountForPagination();
$total = value($total) ?? $this->toBase()->getCountForPagination();

$perPage = ($perPage instanceof Closure
? $perPage($total)
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2727,13 +2727,14 @@ protected function runSelect()
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = $this->getCountForPagination();
$total = value($total) ?? $this->getCountForPagination();

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

Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4450,6 +4450,30 @@ public function testPaginateWithSpecificColumns()
]), $result);
}

public function testPaginateWithTotalOverride()
{
$perPage = 16;
$columns = ['id', 'name'];
$pageName = 'page-name';
$page = 1;
$builder = $this->getMockQueryBuilder();
$path = 'http://foo.bar?page=3';

$results = collect([['id' => 3, 'name' => 'Taylor'], ['id' => 5, 'name' => 'Mohamed']]);

$builder->shouldReceive('getCountForPagination')->never();
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);

Paginator::currentPathResolver(function () use ($path) {
return $path;
});

$result = $builder->paginate($perPage, $columns, $pageName, $page, 10);

$this->assertEquals(10, $result->total());
}

public function testCursorPaginate()
{
$perPage = 16;
Expand Down

0 comments on commit df0135b

Please sign in to comment.