Skip to content

Commit

Permalink
[10.x] allow override of the Builder paginate() total (#46415)
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.

currently handled as a dynamic argument, since we cannot change the method signature on a patch or minor version.

* minor formatting
  • Loading branch information
browner12 authored Mar 25, 2023
1 parent 034d849 commit 350e605
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ 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
Expand All @@ -897,7 +898,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = $this->toBase()->getCountForPagination();
$total = func_num_args() === 5 ? value(func_get_arg(4)) : $this->toBase()->getCountForPagination();

$perPage = ($perPage instanceof Closure
? $perPage($total)
Expand Down
3 changes: 2 additions & 1 deletion 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)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = $this->getCountForPagination();
$total = func_num_args() === 5 ? value(func_get_arg(4)) : $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 @@ -4483,6 +4483,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 350e605

Please sign in to comment.