Skip to content
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
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1114,16 +1114,17 @@ public function pluck($column, $key = null)
* @param array|string $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @param int|null|\Closure $total
* @param array|string $countColumns
* @return \Illuminate\Pagination\LengthAwarePaginator<int, TModel>
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null, $countColumns = ['*'])
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = value($total) ?? $this->toBase()->getCountForPagination();
$total = value($total) ?? $this->toBase()->getCountForPagination($countColumns);

$perPage = value($perPage, $total) ?: $this->model->getPerPage();

Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,13 +989,15 @@ protected function aliasedPivotColumns()
* @param array $columns
* @param string $pageName
* @param int|null $page
* @param int|null|\Closure $total
* @param array $countColumns
Comment on lines +992 to +993
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to narrow this further down

Suggested change
* @param int|null|\Closure $total
* @param array $countColumns
* @param int|null|\Closure(): int $total
* @param array<int, string> $countColumns

or

Suggested change
* @param int|null|\Closure $total
* @param array $countColumns
* @param int|null|\Closure(): int $total
* @param string[] $countColumns

or

Suggested change
* @param int|null|\Closure $total
* @param array $countColumns
* @param int|null|\Closure(): int $total
* @param list<string> $countColumns

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just matching the existing phpdoc style

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's always a chance to improve, but not if one always takes the worst example as their guiding principle 😉

* @return \Illuminate\Pagination\LengthAwarePaginator<int, TRelatedModel&object{pivot: TPivotModel}>
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null, $countColumns = ['*'])
{
$this->query->addSelect($this->shouldSelect($columns));

return tap($this->query->paginate($perPage, $columns, $pageName, $page), function ($paginator) {
return tap($this->query->paginate($perPage, $columns, $pageName, $page, $total, $countColumns), function ($paginator) {
$this->hydratePivotRelation($paginator->items());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,15 @@ public function get($columns = ['*'])
* @param array $columns
* @param string $pageName
* @param int|null $page
* @param int|null|\Closure $total
* @param array $countColumns
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null, $countColumns = ['*'])
{
$this->query->addSelect($this->shouldSelect($columns));

return $this->query->paginate($perPage, $columns, $pageName, $page);
return $this->query->paginate($perPage, $columns, $pageName, $page, $total, $countColumns);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3602,14 +3602,15 @@ protected function withoutGroupLimitKeys($items)
* @param string|\Illuminate\Contracts\Database\Query\Expression|array<string|\Illuminate\Contracts\Database\Query\Expression> $columns
* @param string $pageName
* @param int|null $page
* @param \Closure|int|null $total
* @param int|null|\Closure $total
* @param string|\Illuminate\Contracts\Database\Query\Expression|array<string|\Illuminate\Contracts\Database\Query\Expression> $countColumns
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null, $total = null, $countColumns = ['*'])
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = value($total) ?? $this->getCountForPagination();
$total = value($total) ?? $this->getCountForPagination($countColumns);

$perPage = value($perPage, $total);

Expand Down
9 changes: 6 additions & 3 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6148,6 +6148,7 @@ public function testPaginate()
$columns = ['test'];
$pageName = 'page-name';
$page = 1;
$countColumns = ['test'];
$builder = $this->getMockQueryBuilder();
$path = 'http://foo.bar?page=3';

Expand All @@ -6161,7 +6162,7 @@ public function testPaginate()
return $path;
});

$result = $builder->paginate($perPage, $columns, $pageName, $page);
$result = $builder->paginate($perPage, $columns, $pageName, $page, countColumns: $countColumns);

$this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
'path' => $path,
Expand Down Expand Up @@ -6235,6 +6236,7 @@ public function testPaginateWithSpecificColumns()
$columns = ['id', 'name'];
$pageName = 'page-name';
$page = 1;
$countColumns = ['id'];
$builder = $this->getMockQueryBuilder();
$path = 'http://foo.bar?page=3';

Expand All @@ -6248,7 +6250,7 @@ public function testPaginateWithSpecificColumns()
return $path;
});

$result = $builder->paginate($perPage, $columns, $pageName, $page);
$result = $builder->paginate($perPage, $columns, $pageName, $page, countColumns: $countColumns);

$this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
'path' => $path,
Expand All @@ -6262,6 +6264,7 @@ public function testPaginateWithTotalOverride()
$columns = ['id', 'name'];
$pageName = 'page-name';
$page = 1;
$countColumns = ['id'];
$builder = $this->getMockQueryBuilder();
$path = 'http://foo.bar?page=3';

Expand All @@ -6275,7 +6278,7 @@ public function testPaginateWithTotalOverride()
return $path;
});

$result = $builder->paginate($perPage, $columns, $pageName, $page, 10);
$result = $builder->paginate($perPage, $columns, $pageName, $page, 10, $countColumns);

$this->assertEquals(10, $result->total());
}
Expand Down