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


  • 2017-02-10 15:24:14

    linux学习之——vim简明教程

    学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用。 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力

  • 2017-02-10 16:22:13

    git历史记录查询

    查看提交历史:git log 查看提交历史并显示版本间的差异:git log -p 查看指定历史:git log xxx(sha1值) -p 查看提交历史(指定时间):

  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).

  • 2017-02-16 08:09:01

    HTML中PRE和p的区别

    pre 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。 <pre> 标签的一个常见应用就是用来表示计算机的源代码。