定制 chelout/offset-pagination 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chelout/offset-pagination

最新稳定版本:v1.2

Composer 安装命令:

composer require chelout/offset-pagination

包简介

Offset pagination made easy for Laravel

README 文档

README

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.

Installation

You can install this package via composer using:

composer require chelout/offset-pagination

The package will automatically register itself.

Config

To publish the config file to config/offset_pagination.php run:

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.

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:

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

Or sorting your results:

$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:

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

Calling api/v1 will output:

{
   "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.

    {
        "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:

vendor/bin/phpunit

Credits

License

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

统计信息

  • 总下载量: 4.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固