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

[9.x] Adds ability to have paginate() $perPage parameter as callable with access to $total #42429

Merged
merged 2 commits into from
May 19, 2022
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
15 changes: 10 additions & 5 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public function pluck($column, $key = null)
/**
* Paginate the given query.
*
* @param int|null $perPage
* @param int|null|callable $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
Expand All @@ -876,11 +876,16 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();
$total = $this->toBase()->getCountForPagination();

$perPage = ($perPage instanceof Closure
? $perPage($total)
: $perPage
) ?: $this->model->getPerPage();

$results = ($total = $this->toBase()->getCountForPagination())
? $this->forPage($page, $perPage)->get($columns)
: $this->model->newCollection();
$results = $total
? $this->forPage($page, $perPage)->get($columns)
: $this->model->newCollection();

return $this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2634,7 +2634,7 @@ protected function runSelect()
/**
* Paginate the given query into a simple paginator.
*
* @param int $perPage
* @param int|callable $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
Expand All @@ -2646,6 +2646,8 @@ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $p

$total = $this->getCountForPagination();

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

$results = $total ? $this->forPage($page, $perPage)->get($columns) : collect();

return $this->paginator($results, $total, $perPage, $page, [
Expand Down
63 changes: 63 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,69 @@ public function testPaginatedModelCollectionRetrieval()
$this->assertSame('[email protected]', $models[0]->email);
}

public function testPaginatedModelCollectionRetrievalUsingCallablePerPage()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']);
EloquentTestUser::create(['id' => 3, 'email' => '[email protected]']);

Paginator::currentPageResolver(function () {
return 1;
});
$models = EloquentTestUser::oldest('id')->paginate(function ($total) {
return $total <= 3 ? 3 : 2;
});

$this->assertCount(3, $models);
$this->assertInstanceOf(LengthAwarePaginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertInstanceOf(EloquentTestUser::class, $models[2]);
$this->assertSame('[email protected]', $models[0]->email);
$this->assertSame('[email protected]', $models[1]->email);
$this->assertSame('[email protected]', $models[2]->email);

Paginator::currentPageResolver(function () {
return 2;
});
$models = EloquentTestUser::oldest('id')->paginate(function ($total) {
return $total <= 3 ? 3 : 2;
});

$this->assertCount(0, $models);
$this->assertInstanceOf(LengthAwarePaginator::class, $models);

EloquentTestUser::create(['id' => 4, 'email' => '[email protected]']);

Paginator::currentPageResolver(function () {
return 1;
});
$models = EloquentTestUser::oldest('id')->paginate(function ($total) {
return $total <= 3 ? 3 : 2;
});

$this->assertCount(2, $models);
$this->assertInstanceOf(LengthAwarePaginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertSame('[email protected]', $models[0]->email);
$this->assertSame('[email protected]', $models[1]->email);

Paginator::currentPageResolver(function () {
return 2;
});
$models = EloquentTestUser::oldest('id')->paginate(function ($total) {
return $total <= 3 ? 3 : 2;
});

$this->assertCount(2, $models);
$this->assertInstanceOf(LengthAwarePaginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertSame('[email protected]', $models[0]->email);
$this->assertSame('[email protected]', $models[1]->email);
}

public function testPaginatedModelCollectionRetrievalWhenNoElements()
{
Paginator::currentPageResolver(function () {
Expand Down