laravel自定义分页LengthAwarePaginator

2019-12-12 14:19:32

有时候我们使用larave提供的后台分页数据库查询,有时候限制太多,我们需要自己定制分页功能。

下面是我给大家一个例子,我们可以根据例子,制作自己的分页功能。

    use Illuminate\Pagination\LengthAwarePaginator;
    use Illuminate\Pagination\Paginator;
    use Illuminate\Http\Request;
# 仅做演示 #
   public function userList(Request $request) {
        $users = [
            ['username'=>'zhangsan', 'age'=>26],
            ['username'=>'lisi', 'age'=>23],
            ['username'=>'wangwu', 'age'=>62],
            ['username'=>'zhaoliu', 'age'=>46],
            ['username'=>'wangmazi', 'age'=>25],
            ['username'=>'lanzi', 'age'=>24],
            ['username'=>'pangzi', 'age'=>21]
        ];
        $perPage = 3;
        if ($request->has('page')) {
            $current_page = $request->input('page');
            $current_page = $current_page <= 0 ? 1 :$current_page;
        } else {
            $current_page = 1;
        }
        $item = array_slice($users, ($current_page-1)*$perPage, $perPage); //注释1
        $total = count($users);
        $paginator =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [
            'path' => Paginator::resolveCurrentPath(), //注释2
            'pageName' => 'page',
        ]);
        $userlist = $paginator->toArray()['data'];
        return view('userlist', compact('userlist', 'paginator'));
    }

本场景代码
public function index(Request $request){
    $paginator =new LengthAwarePaginator([], 100,5, 1, [
        'path' => Paginator::resolveCurrentPath(), //注释2
        'pageName' => 'page',
    ]);
    echo  $paginator->appends(['sort' => 'votes'])->render();
    exit;

————————————————
版权声明:本文为CSDN博主「wangxiaoangg」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16399991/article/details/56676793


  • 2019-04-30 11:23:36

    elasticsearch和analysis-ik的安装使用

    全文搜索和中文分词主要介绍了两组全文搜索加中文分词方案; TNTSearch+jieba-php这套组合对于博客这类的小项目基本够用了;

  • 2019-04-30 11:42:24

    php7+laravel+coreseek(sphinx)中文搜索初步实现(Linux)

    官网www.coreseek.cn已不能下载,所以需从网上找资源, 注意的一点是,笔者安装coreseek-3.2.14版本后,使用时提示client版本高于server版本的错误, php的sphinx扩展,为使用者,为client;coreseek是系统服务,为server

  • 2019-04-30 13:55:13

    浅谈mysql fulltext全文索引优缺点

    为什么会注意到mysql的fulltext? nima, 还是上次innodb转成tokudb引擎的事,这次alter修改表引擎的时候,提示percona tokudb是不支持fulltext索引的.

  • 2019-04-30 18:56:52

    elasticsearch文档操作

    使用了Elasticsearch提供的一整套强大的REST API,本文继续来看通过这一套API如何完成文档的基本操作。