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

[10.x] allow override of the Builder paginate() total #46415

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