2017 © Pedro Peláez
 

library offset-pagination

Offset pagination made easy for Laravel

image

chelout/offset-pagination

Offset pagination made easy for Laravel

  • Friday, April 20, 2018
  • by chelout
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 36 % Grown

The README.md

This package provides an offset based pagination already integrated with Laravel's query builder and Eloquent ORM. It calculates the SQL query limits automatically by checking the requests GET parameters, and automatically builds the next and previous urls for you., (*1)

Installation

You can install this package via composer using:, (*2)

composer require chelout/offset-pagination

The package will automatically register itself., (*3)

Config

To publish the config file to config/offset_pagination.php run:, (*4)

php artisan vendor:publish --provider="Chelout\OffsetPagination\CursorPaginationServiceProvider" --tag="config"

This will publish the following file. You can customize default items per page and maximum items per page., (*5)

Basic Usage

Paginating Query Builder Results

public function index()
{
    $users = DB::table('users')->offsetPaginate();

    return $users;
}

Paginating Eloquent Results

$users = User::offsetPaginate(5);

Of course, you may call paginate after setting other constraints on the query, such as where clauses:, (*6)

$users = User::where('votes', '>', 100)->offsetPaginate(5);

Or sorting your results:, (*7)

$users = User::orderBy('id', 'desc')->offsetPaginate(5);

Displaying Pagination Results

Converting to JSON

A basic return will transform the paginator to JSON and will have a result like this:, (*8)

Route::get('api/v1/users', function () {
    return App\User::offsetPaginate();
});

Calling api/v1 will output:, (*9)

{
   "data": [
        {}, 
   ],
    "offset": 60,
    "offset": 60,
    "prev": 55,
    "next": 65,
    "limit": 5,
    "total": 100,
    "next_page_url": "https://example.com/api/v1/users?limit=5&offset=65",
    "prev_page_url": "https://example.com/api/v1/users?limit=5&offset=55"
}

Using a Resource Collection

By default, Laravel's API Resources when using them as collections, they will output a paginator's metadata into links and meta., (*10)

    {
        "data":[
            {}, 
        ],
        "links": {
            "first": "https://example.com/api/v1/users",
            "last": "https://example.com/api/v1/users?offset=95",
            "prev": "https://example.com/api/v1/users?offset=55",
            "next": "https://example.com/api/v1/users?offset=65"
        },
        "meta": {
            "offset": 60,
            "prev": 55,
            "next": 65,
            "limit": 5,
            "total": 100
        },
    }

Testing

Run the tests with:, (*11)

vendor/bin/phpunit

Credits

License

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

The Versions

20/04 2018

dev-master

9999999-dev https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

20/04 2018

dev-analysis-z30a4A

dev-analysis-z30a4A https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

12/03 2018

1.0.3

1.0.3.0 https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

28/02 2018

1.0.2

1.0.2.0 https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

27/02 2018

1.0.1

1.0.1.0 https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

27/02 2018

1.0.0

1.0.0.0 https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination

27/02 2018

0.1.0

0.1.0.0 https://github.com/chelout/offset-pagination

Offset pagination made easy for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar chelout

laravel api pagination