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


  • 2021-02-19 16:46:35

    window安装composer

    Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。

  • 2021-02-21 22:52:12

    php去除字符串中的HTML标签

    php自带的函数可以去除/删除字符串中的HTML标签/代码。 strip_tags(string,allow):函数剥去 HTML、XML 以及 PHP 的标签。 参数:string,必填,规定要检查的字符串;allow,选填,规定允许存在的标签,这些标签不会被删除。

  • 2021-03-02 16:00:30

    git pull时的filename too long的错误

    这是因为git在windowa下的文件名长度最大是260,(git在Linux下最大支持4096长度的文件名),可以通过输入以下命令解决:

  • 2021-03-05 13:18:03

    mjml教程详解

    mjml如何快速编写响应式电子邮件

  • 2021-03-15 10:34:55

    Sass函数:Sass Maps的函数-map-get($map,$key)

    map-get($map,$key) 函数的作用是根据 $key 参数,返回 $key 在 $map 中对应的 value 值。如果 $key 不存在 $map中,将返回 null 值。此函数包括两个参数: