zoukankan      html  css  js  c++  java
  • laravel自定义分页可以在get()获取数据后使用LengthAwarePaginator()类进行分页

    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;

    原文链接:https://blog.csdn.net/qq_16399991/article/details/56676793

  • 相关阅读:
    IOS:APP网络状态的检测
    IOS:个人笔记|UI__使用Plist文件来进行数据的读取
    IntelliJ IDEA中项目import与open的区别
    打开电脑分屏
    微服务
    自增主键和UUID
    雪花算法
    使用navicat连接阿里云上mysql
    【vue】报错This dependency was not found
    跨域问题
  • 原文地址:https://www.cnblogs.com/xiaofeilin/p/15014555.html
Copyright © 2011-2022 走看看