laravel 手动分页,应用场景,需要在分页数据中传递特殊参数,laravel自带paginate方法不满足的情况下。
初始数据如下:
$data = array(
['id'=>'1','user_id'=>2,'papaer_id'=>10],
['id'=>'2','user_id'=>2,'papaer_id'=>11],
);
转为json的格式如下:
[
{
"id": "1",
"user_id": 2,
"papaer_id": 10
},
{
"id": "2",
"user_id": 2,
"papaer_id": 11
}
]
这里一共2条数据,我们设定每页显示1个,一共2页。
我们先看下laravel自带方法,给我带来的效果。
{
"total": 2,
"per_page": 1,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://127.0.0.1:8999/page?page=2",
"prev_page_url": null,
"from": 1,
"to": 1,
"data": [
{
"id": 1,
"user_id": 2,
"paper_id": 10
}
]
}
我想在其中加自定义参数,比如这个路由的URL,已达到如下效果(根据自己所需加参数):
{
“path”: "http://127.0.0.1:8999",
"total": 2,
"per_page": 1,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://127.0.0.1:8999/page?page=2",
"prev_page_url": null,
"from": 1,
"to": 1,
"data": [
{
"id": 1,
"user_id": 2,
"paper_id": 10
}
]
}
首先我们看下laravel得分页方法源码:
#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); }
控制器中,分页方法调用:
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; public function show(Request $request){
$blogs = DB::table('blog')->where('uid',$uid)->select(); // 假设查出的数据
$perPage = 1; // 每页显示数量 if ($request->has('page')) { // 请求是第几页,如果没有传page数据,则默认为1 $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; }
$item = array_slice($real, ($current_page-1)*$perPage, $perPage); // 注释1 $total = count($blogs); // 查询总数 $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [ 'path' => Paginator::resolveCurrentPath(), // 注释2 'pageName' => 'page', ]);
return response()->json(['result'=>$paginator]) }
注释1: array_slice(array,start,length) 从start位置,去获取length参数。结果返回一条数据
注释2:就是设定个要分页的url地址。也可以手动通过 $paginator ->setPath(‘路径’) 设置。
页面中的分页连接也是同样的调用方式 {{ $paginator->render() }}。
注:返回得数据格式大概如下所示:
{
“path”: "http://127.0.0.1:8999",
"total": 2,
"per_page": 1,
"current_page": 1,
"last_page": 2,
"next_page_url": "http://127.0.0.1:8999/page?page=2",
"prev_page_url": null,
"from": 1,
"to": 1,
"data": [
{
"id": 1,
"user_id": 2,
"paper_id": 10
}
]
}
如果,这篇文章帮到了你,欢迎点击推荐。有疑问,请评论。