关于laravel 得手动分页问题

      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
    }
  ]
}

 



如果,这篇文章帮到了你,欢迎点击推荐。有疑问,请评论。

转载于:https://www.cnblogs.com/jingying/p/7131008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值