标签:item div view oar 实例化 offset return fse new
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(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‘)); } //视图中 {{ $data->links() }}
标签:item div view oar 实例化 offset return fse new
原文地址:https://www.cnblogs.com/niuben/p/12378265.html