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 simplePaginate() $perPage parameter as callable with access to $total #42840

Closed
wants to merge 1 commit into from
Closed
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: 11 additions & 4 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
/**
* Paginate the given query into a simple paginator.
*
* @param int|null $perPage
* @param int|null|\Closure $perPage
* @param array|string $columns
* @param string $pageName
* @param int|null $page
Expand All @@ -906,14 +906,21 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

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

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

// Next we will set the limit and offset for this query so that when we get the
// results we get the proper section of results. Then, we'll create the full
// paginator instances for these results with the given page and per page.
$this->skip(($page - 1) * $perPage)->take($perPage + 1);
$results = $total
? $this->skip(($page - 1) * $perPage)->take($perPage + 1)->get($columns)
: $this->model->newCollection();

return $this->simplePaginator($this->get($columns), $perPage, $page, [
return $this->simplePaginator($results, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
Expand Down
10 changes: 7 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2661,7 +2661,7 @@ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $p
*
* This is more efficient on larger data-sets, etc.
*
* @param int $perPage
* @param int|callable $perPage
* @param array|string $columns
* @param string $pageName
* @param int|null $page
Expand All @@ -2671,9 +2671,13 @@ public function simplePaginate($perPage = 15, $columns = ['*'], $pageName = 'pag
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$this->offset(($page - 1) * $perPage)->limit($perPage + 1);
$total = $this->getCountForPagination();

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

$results = $total ? $this->offset(($page - 1) * $perPage)->limit($perPage + 1)->get($columns) : collect();

return $this->simplePaginator($this->get($columns), $perPage, $page, [
return $this->simplePaginator($results, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
Expand Down
62 changes: 62 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,68 @@ public function testPaginatedModelCollectionRetrievalUsingCallablePerPage()
$this->assertSame('bar@gmail.com', $models[1]->email);
}

public function testSimplePaginatedModelCollectionRetrievalUsingCallablePerPage()
{
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
EloquentTestUser::create(['id' => 3, 'email' => 'foo@gmail.com']);

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

$this->assertCount(3, $models);
$this->assertInstanceOf(Paginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertInstanceOf(EloquentTestUser::class, $models[2]);
$this->assertSame('taylorotwell@gmail.com', $models[0]->email);
$this->assertSame('abigailotwell@gmail.com', $models[1]->email);
$this->assertSame('foo@gmail.com', $models[2]->email);

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

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

EloquentTestUser::create(['id' => 4, 'email' => 'bar@gmail.com']);

Paginator::currentPageResolver(function () {
return 1;
});
$models = EloquentTestUser::oldest('id')->simplePaginate(function ($total) {
return $total <= 3 ? 3 : 2;
});
$this->assertCount(2, $models);
$this->assertInstanceOf(Paginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertSame('taylorotwell@gmail.com', $models[0]->email);
$this->assertSame('abigailotwell@gmail.com', $models[1]->email);

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

$this->assertCount(2, $models);
$this->assertInstanceOf(Paginator::class, $models);
$this->assertInstanceOf(EloquentTestUser::class, $models[0]);
$this->assertInstanceOf(EloquentTestUser::class, $models[1]);
$this->assertSame('foo@gmail.com', $models[0]->email);
$this->assertSame('bar@gmail.com', $models[1]->email);
}

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