• laravel手动数组分页


    laravel文档中已经有写如何自己使用分页类去分页了,但没有详细说明。

    如果你想手动创建分页实例并且最终得到一个数组类型的结果,可以根据需求来创建 IlluminatePaginationPaginator 或者 IlluminatePaginationLengthAwarePaginator 实例来实现。

    具体可以看IlluminatePaginationLengthAwarePaginator中的这段代码:

    public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
     {
      foreach ($options as $key => $value) {
       $this->{$key} = $value;
      }
     
      $this->total = $total;
      $this->perPage = $perPage;
      $this->lastPage = max((int) ceil($total / $perPage), 1);
      $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
      $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
      $this->items = $items instanceof Collection ? $items : Collection::make($items);
     }

    以下为具体实现代码:

    public function index(Request $request)
     {
      //数据A
      $dataA = User::where('status', 1)->get()->toArray();
      //数据B
      $dataB = User::where('status', 2)->get()->toArray();
      $data = array_merge($dataA, $dataB);
      //当前页数 默认1
      $page = $request->page ?: 1;
      //每页的条数
      $perPage = 4;
      //计算每页分页的初始位置
      $offset = ($page * $perPage) - $perPage;
       //实例化LengthAwarePaginator类,并传入对应的参数
      $data = new LengthAwarePaginator(array_slice($data, $offset, $perPage, true), count($data), $perPage,
       $page, ['path' => $request->url(), 'query' => $request->query()]);
      return view('admin.users.index', compact('data'));
     }
     
    //视图中
    {{ $data->links() }}
  • 相关阅读:
    [网鼎杯 2018]Comment-Git泄露部分
    Google Hacking 详解
    macOS修改Docker容器的端口映射配置
    CentOS6 7 8更换阿里yum源
    XSS代码合集(含测试效果详细版)-HTML4与更早版本的向量2
    VMware 启动Ubuntu时黑屏
    XSS代码合集(含测试效果详细版)-HTML4与更早版本的向量1
    APP安全在线检测网站
    Juice-Shop 二星题
    慕课网-安卓工程师初养成-4-5 练习题
  • 原文地址:https://www.cnblogs.com/niuben/p/12378265.html
Copyright © 2020-2023  润新知