Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Paginator total override #46410

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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