Laravel 5和长度感知分页 [英] Laravel 5 and Length Aware Pagination

查看:105
本文介绍了Laravel 5和长度感知分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有另一个分页问题.当我遍历下面的分页数组时,每页都会得到整个数组.

I've got another pagination problem it looks like. When I iterate over the paginated array below, I get the entire array every page.

$array = [...];
$ret = new LengthAwarePaginator($array, count($array), 10);
// dd($ret);

LengthAwarePaginator {#302 ▼
  #total: 97
  #lastPage: 10
  #items: Collection {#201 ▼
    #items: array:97 [▶]
  }
  #perPage: 10
  #currentPage: 1
  #path: "/"
  #query: []
  #fragment: null
  #pageName: "page"
}

从雄辩的模型构建LAP时不是这种情况,例如:Blah::paginate()

This isn't the case when building a LAP from an eloquent model eg: Blah::paginate()

推荐答案

分页器不会自动为您分割给定的数组.您必须先将其切片,然后再将其传递给分页器.

The paginator does not slice the given array for you automatically. You have to slice it yourself before you pass it to the paginator.

要使您的生活更轻松,请使用collect助手来创建laravel集合的实例,这使得分割非常容易:

To make life easier for you, use the collect helper to create an instance of a laravel collection, which makes it very easy to slice up:

$items = collect([...]);
$page = Input::get('page', 1);
$perPage = 10;

$paginator = new LengthAwarePaginator(
    $items->forPage($page, $perPage), $items->count(), $perPage, $page
);

这篇关于Laravel 5和长度感知分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆