码迷,mamicode.com
首页 > 其他好文 > 详细

laravel 手动对数据分页

时间:2020-05-06 12:04:43      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:paginator   php   etc   merge   page   get   int   erp   tab   

手动创建分页

如果你想手动创建分页实例并且最终得到一个数组类型的结果,可以根据需求来创建 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(LiveService $liveService, 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‘));
 }

  

laravel 手动对数据分页

标签:paginator   php   etc   merge   page   get   int   erp   tab   

原文地址:https://www.cnblogs.com/cbywan/p/12835271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!