• 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() }}
  • 相关阅读:
    Toad 常用快捷键
    Oracle Form删除list项
    不得重新使用的登录口令
    OE_ORDER_PUB.PROCESS_ORDER to Release a hold on sales order in R12
    OE_ORDER_PUB.PROCESS_ORDER to Apply hold on a sales order
    说明性弹性域段
    使用VPD解决EBS中信息屏蔽问题
    Oracle EBS客户化程序中格式化金额
    Form开发中组件控制的几个常用方法
    .Net的差评
  • 原文地址:https://www.cnblogs.com/niuben/p/12378265.html
Copyright © 2020-2023  润新知