关于laravel 得手动分页问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于laravel 得手动分页问题相关的知识,希望对你有一定的参考价值。

一般得分页,我们只需要使用paginate方法,就可以简单得搞定。但是遇到数组得组合情况呢?这个时候,就需要我们使用自定义分页了。首先我们看下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);
}

假定分页得数组为:
[
  {
    "id": 9,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  },
  {
    "id": 5,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  },
  {
    "id": 1,
    "sys_id": 1,
    "org_id": 2,
    "user_id": 8,
    "papaer_id": 26,
  }
]
这里一共3条数据,我们设定每页显示1个,一共3页。
分页调用
use Illuminate\Pagination\LengthAwarePaginator;
  use Illuminate\Pagination\Paginator;

  public function show(Request $request){
$perPage = 1; //每页显示数量
        if ($request->has(‘page‘)) {
                $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($real);

        $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [
                ‘path‘ => Paginator::resolveCurrentPath(),  //注释2
                ‘pageName‘ => ‘page‘,
        ]);
  return response()->json([‘result‘=>$paginator])
}

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

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

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

  注:返回得数据格式大概如下所示:

"result": {
    "current_page": 3,
    "data": [
    {
      "id": 5,
      "sys_id": 1,
      "org_id": 2,
      "user_id": 8,
"papaer_id": 26
     }
], "from": null, "last_page": 2, "next_page_url": null, "path": "http://www.text.tld/examination/show", "per_page": 2, "prev_page_url": "http://www.text.tld/examination/show?page=2", "to": null, "total": 3 }







































以上是关于关于laravel 得手动分页问题的主要内容,如果未能解决你的问题,请参考以下文章

laravel 手动对数据分页

Laravel 5 - 手动分页

手动分页错误 - Laravel 5.0

laravel手动数组分页

Laravel 手动分页实现

如何在 Laravel 5 中对合并的集合进行分页?