Laravel 手动分页实现

基于5.2版本

在开发过程中有这么一种情况,你请求Java api获取信息,由于信息较多,需要分页显示。Laravel官方提供了一个简单的方式paginate($perPage),但是这种方法只适用model、查询构建器。 
今天说下 给定一个数组如何实现 和paginate方法一样的效果。

查看paginate方法源码

#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$query = $this->toBase(); $total = $query->getCountForPagination(); $this->forPage(
$page = $page ?: Paginator::resolveCurrentPage($pageName),
$perPage = $perPage ?: $this->model->getPerPage()
); return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}

从上面就可以看出,分页的关键就在于LengthAwarePaginator。

LengthAwarePaginator的构造方法。

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 = (int) ceil($total / $perPage);
$this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}

其实已经很明白了,假如要分页的数组为

[
['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],
]

共7条数据,每页显示3条,共3页

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
# 仅做演示 #
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'));
}

上面的代码中的重点是$item,如果不做注释1处理,得出的是所有7条数据。

注释2处就是设定个要分页的url地址。也可以手动通过 $paginator ->setPath(‘路径’) 设置。

页面中的分页连接也是同样的调用方式 {{ $paginator->render() }}

好了,基本就是这样,有纰漏的地方欢迎指正!

转:https://blog.csdn.net/hxx_yang/article/details/51753134

最新文章

  1. JSP实现在项目在网页上查询
  2. Swap Swap,即交换分区
  3. [POJ1284]Primitive Roots(原根性质的应用)
  4. archlinux 学习笔记
  5. beta版本工作百分比
  6. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
  7. hdu 2058
  8. 关于 MVCC 的基础
  9. PHP字符串三种定义方式
  10. 微信小程序,前端大梦想(七)
  11. 用python给html里的css及js文件链接自动添加版本号
  12. win 10 精简组件列表
  13. Win 10 Revit 2019 安装过程,亲自踩的一遍坑,有你想要的细节
  14. 对多字段进行去重 ( Linq 方式 )
  15. LCA的在线与离线算法
  16. Lintcode - 20.骰子求和
  17. 通过PHP调用微信JSSDK实例
  18. 在线图标制作,格式转换 ICON
  19. 本地服务器搭建服务:svn
  20. Netty网络聊天室之会话管理

热门文章

  1. CentOS 6.9下双网卡绑定单个IP地址及装网卡绑定到一个网桥(转)
  2. golang slice 切片原理
  3. Android-25种开源炫酷动画框架
  4. 阻止jQuery事件冒泡
  5. ZOJ3622 Magic Number(水题)
  6. eslint 错误
  7. 细说linux IPC(四):posix 共享内存
  8. JedisCluster操作redis集群demo
  9. Entity Framework底层操作封装V2版本号(1)
  10. ionic 图片加载失败,显示默认图片代替