Skip to content

Commit aaee592

Browse files
authored
Merge pull request #79 from sebj54/main
Add `$totalResults` parameter + update docs
2 parents ea6a88b + edcbefd commit aaee592

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ $model = YourModel::find(1);
131131
$model->relation()->jsonPaginate();
132132
```
133133

134+
### Override default behavior
135+
134136
By default the maximum page size is set to 30. You can change this number in the `config` file or just pass the value to `jsonPaginate`.
135137

136138
```php
@@ -139,6 +141,24 @@ $maxResults = 60;
139141
YourModel::jsonPaginate($maxResults);
140142
```
141143

144+
By default the default page size is set to 30. You can change this number in the `config` file or just pass the value to `jsonPaginate`.
145+
146+
```php
147+
$defaultSize = 15;
148+
149+
YourModel::jsonPaginate(null, $defaultSize);
150+
```
151+
152+
You can also pass the total count to the `paginate` function directly. This can be useful for performance reasons or to prevent issues with `DISTINCT` keyword ([more info](https://github.com/laravel/framework/issues?q=is%3Aissue+paginate+total)).
153+
154+
⚠️ This is effective only with basic pagination (no effect with cursor, simple or fast pagination)
155+
156+
```php
157+
$total = 42;
158+
159+
YourModel::jsonPaginate(null, null, $total);
160+
```
161+
142162
### Cursor pagination
143163

144164
This package also supports cursor pagination, which can be briefly defined by the Laravel Framework as follows:

src/JsonApiPaginateServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function register()
3030

3131
protected function registerMacro()
3232
{
33-
$macro = function (int $maxResults = null, int $defaultSize = null) {
33+
$macro = function (?int $maxResults = null, ?int $defaultSize = null, ?int $totalResults = null) {
3434
$maxResults = $maxResults ?? config('json-api-paginate.max_results');
3535
$defaultSize = $defaultSize ?? config('json-api-paginate.default_size');
3636
$numberParameter = config('json-api-paginate.number_parameter');
@@ -45,7 +45,7 @@ protected function registerMacro()
4545
: (config('json-api-paginate.use_fast_pagination') ? 'fastPaginate' : 'paginate')
4646
);
4747

48-
if (config('json-api-paginate.use_fast_pagination') && !InstalledVersions::isInstalled('hammerstone/fast-paginate')) {
48+
if (config('json-api-paginate.use_fast_pagination') && ! InstalledVersions::isInstalled('hammerstone/fast-paginate')) {
4949
abort(500, 'You need to install hammerstone/fast-paginate to use fast pagination.');
5050
}
5151

@@ -64,7 +64,7 @@ protected function registerMacro()
6464
? $this->{$paginationMethod}($size, ['*'], $paginationParameter.'['.$cursorParameter.']', $cursor)
6565
->appends(Arr::except(request()->input(), $paginationParameter.'.'.$cursorParameter))
6666
: $this
67-
->{$paginationMethod}($size, ['*'], $paginationParameter.'.'.$numberParameter)
67+
->{$paginationMethod}($size, ['*'], $paginationParameter.'.'.$numberParameter, null, $totalResults)
6868
->setPageName($paginationParameter.'['.$numberParameter.']')
6969
->appends(Arr::except(request()->input(), $paginationParameter.'.'.$numberParameter));
7070

0 commit comments

Comments
 (0)