https://github.com/ericksonreyes/pagination
Simple Pagination Class for PHP 8.1
https://github.com/ericksonreyes/pagination
Last synced: 3 months ago
JSON representation
Simple Pagination Class for PHP 8.1
- Host: GitHub
- URL: https://github.com/ericksonreyes/pagination
- Owner: ericksonreyes
- License: mit
- Created: 2022-12-19T09:40:51.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-06-20T23:50:13.000Z (almost 2 years ago)
- Last Synced: 2025-01-05T19:15:06.511Z (5 months ago)
- Language: PHP
- Size: 121 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Pagination Class for PHP 8

[](https://github.com/ericksonreyes/pagination/actions/workflows/merge.yaml)Nothing fancy. I just created a pagination class that I've been copy-pasting over and over again.
## Installation
```shell
composer require ericksonreyes/pagination
```### Example (Laravel)
Controller
```php
namespace App\Http\Controllers;use App\Repository\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;class Users extends BaseController {
private const DEFAULT_PAGE_SIZE = 35;
public function index(Request $request, UserRepository $repository): Response {
$page = (int) $request->get('page', 1);
$size = (int) $request->get('size', self::DEFAULT_PAGE_SIZE);
if ($page < 1) {
$page = 1;
}
if ($size < 1) {
$size = self::DEFAULT_PAGE_SIZE;
}
$offset = $page - 1;
$limit = $size;
$count = $repository->countUsers();
$data['users'] = $repository->getUsers($offset, $limit);
$data['pagination'] = new Pagination(
recordsFound: $count,
recordsPerPage: 10,
currentPage: $page
);
return response()->view('list', $data);
}
}
```View (Blade Templating)
```php
@if(isset($pagination) && $pagination->hasPages())
-
Previous
-
{{ $pagination->firstPage() }}
- ...
- {{ $page }}
-
{{ $page }}
- ...
-
{{ $pagination->lastPage() }}
-
Next
@if($pagination->hasPreviousPage())
@endif
@if($pagination->hasFirstPage())
@endif
@foreach($pagination->pages() as $page)
@if($pagination->currentPage() === $page)
@else
@endif
@endforeach
@if($pagination->hasLastPage())
@endif
@if($pagination->hasNextPage())
@endif
@endif
```
View (Vanilla PHP)
```php
hasPages()) {
?>
- hasPreviousPage()) {
-
Previous
-
firstPage() ?>
- ...
-
-
- ...
-
lastPage() ?>
-
Next
?>
hasFirstPage()) {
?>
pages() as $page) {
if($pagination->currentPage() === $page) {
?>
hasLastPage()) {
?>
hasNextPage()) {
?>
```
### Author
* Erickson
Reyes ([GitHub](https://github.com/ericksonreyes), [GitLab](https://gitlab.com/ericksonreyes/), [LinkedIn](https://www.linkedin.com/in/ericksonreyes/)
and [Packagist](http://packagist.org/users/ericksonreyes/)).
### License
See [LICENSE](LICENSE)
### Gitlab
This project is also available in [GitLab](https://gitlab.com/ericksonreyes/pagination)