acadea/collection-paginator

Laravel Package to paginate any collection

v1.0.1 2021-01-14 06:53 UTC

This package is auto-updated.

Last update: 2024-11-14 15:16:55 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A helper package to paginate Laravel collections.

About

Learn the idea behind this package:

Follow us on Youtube: Acadea.io

Installation

You can install the package via composer:

composer require acadea/collection-paginator

Usage

$collection = collect([1,2,3,4,5,6,7,8]);

$pageSize = 4;

$paginated = \Acadea\CollectionPaginator\CollectionPaginator::paginate($collection, $pageSize);

// ..

// in resource controller, returning as an api response
return new \Illuminate\Http\JsonResponse($paginated);

Expects:

{
    "current_page": 1,
    "data": [
        1,
        2,
        3,
        4
    ],
    "first_page_url": "http://localhost:8000/api/fruits?page=1",
    "from": 1,
    "last_page": 2,
    "last_page_url": "http://localhost:8000/api/fruits?page=2",
    "links": [
        {
            "url": null,
            "label": "Previous",
            "active": false
        },
        {
            "url": "http://localhost:8000/api/fruits?page=1",
            "label": 1,
            "active": true
        },
        {
            "url": "http://localhost:8000/api/fruits?page=2",
            "label": 2,
            "active": false
        },
        {
            "url": "http://localhost:8000/api/fruits?page=2",
            "label": "Next",
            "active": false
        }
    ],
    "next_page_url": "http://localhost:8000/api/fruits?page=2",
    "path": "http://localhost:8000/api/fruits",
    "per_page": 4,
    "prev_page_url": null,
    "to": 4,
    "total": 8
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.