Skip to content

Commit 05b1aa0

Browse files
committed
- Added pagination docs
1 parent 467d4d8 commit 05b1aa0

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

docs/basic-usage/pagination.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Pagination
3+
sidebar_position: 3
4+
---
5+
6+
By default, the `index` endpoint of API controllers are paginated to `10` results per page.
7+
Users can use the `?per_page=` query parameter to specify how many items to return per page.
8+
9+
## Modifying the default results per page
10+
11+
You can modify the default `per_page` amount of `10` by overriding the `getDefaultPerPage` method.
12+
13+
```php
14+
public function getDefaultPerPage(): int
15+
{
16+
return 20;
17+
}
18+
```
19+
20+
## Modifying the max results allowed per page
21+
22+
By default the max `per_page` value allowed is `50`.
23+
You can modify this by overriding the `getMaxPerPage` method.
24+
25+
```php
26+
public function getMaxPerPage(): int
27+
{
28+
return 100;
29+
}
30+
```
31+
32+
## Allowing unlimited results
33+
34+
:::danger
35+
36+
Allowing this option can expose all of your records to the user in a single request.
37+
This can hit your performance if there are a lot of records for the model.
38+
39+
:::
40+
41+
If you want the user to be able to return all results, without any pagination, override `allowUnlimitedResultsPerPage` method.
42+
43+
```php
44+
public function allowUnlimitedResultsPerPage(): bool
45+
{
46+
return true;
47+
}
48+
```
49+
50+
Now, if the user sets `?per_page=-1`, then all results will be returned without any pagination.
51+

src/Concerns/IsApiController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function allowUnlimitedResultsPerPage(): bool
4949
*/
5050
protected function wantsAllResults(Request $request): bool
5151
{
52-
return $request->input('per_page') == -1;
52+
return $request->input('per_page', $this->getDefaultPerPage()) == -1;
5353
}
5454

5555
/**
@@ -64,6 +64,16 @@ protected function getPerPage(Request $request, int $default = 0)
6464
return abs($request->input('per_page', $default));
6565
}
6666

67+
public function getDefaultPerPage(): int
68+
{
69+
return 10;
70+
}
71+
72+
public function getMaxPerPage(): int
73+
{
74+
return 50;
75+
}
76+
6777
protected function getQueryBuilder(): QueryBuilder
6878
{
6979
return QueryBuilder::for($this->getBaseQuery());
@@ -100,7 +110,7 @@ protected function indexEndpoint(Request $request)
100110
return $query->get();
101111
}
102112

103-
return $query->paginate($this->getPerPage($request, 10))
113+
return $query->paginate($this->getPerPage($request, $this->getDefaultPerPage()))
104114
->appends($request->except(['page']));
105115
}
106116

@@ -169,7 +179,7 @@ protected function authorizeView(Model $model): void
169179
protected function getIndexValidation(): array
170180
{
171181
return [
172-
'per_page' => "integer|" . ($this->allowUnlimitedResultsPerPage() ? 'min:-1' : 'between:1,50'),
182+
'per_page' => "integer|" . ($this->allowUnlimitedResultsPerPage() ? 'min:-1' : 'between:1,' . $this->getMaxPerPage()),
173183
'page' => 'integer|min:1',
174184
];
175185
}

0 commit comments

Comments
 (0)