简体   繁体   English

LengthAwarePaginator $ collection-> lastPage()始终为1

[英]LengthAwarePaginator $collection->lastPage() always 1

I'm using Laravel 5.2 and LengthAwarePaginator to paginate my Collection, everything goes well except the lastPage() method, it gives me 1 as always. 我正在使用Laravel 5.2和LengthAwarePaginator对我的Collection进行分页,除lastPage()方法外,其他一切都进行得很好,它像往常一样给了我1。

Here is my code: 这是我的代码:

I build the collection by using foreach loop: 我通过使用foreach循环来构建集合:

$collection = collect();    
foreach ($rows as $row) {
    $collection->push($row);
}

then I paginate it using: 然后,我用它分页

$collection = $collection->sortBy('id')->forPage($page, $page_limit);
$collection = new LengthAwarePaginator($collection, $collection->count(), $page_limit, $page);
return $collection;

Here is my pagination information: 这是我的分页信息:

  1. ->currentPage(): 3 (I am on page 3 right now) -> currentPage():3(我现在在第3页上)
  2. $per_page_limit: 8 $ per_page_limit:8
  3. Total item in Collection: 77 集合总数:77
  4. ->lastPage(): 1 (this is the problem) -> lastPage():1(这是问题)

My question is why ->lastPage() value is always 1 ? 我的问题是为什么-> lastPage()的值始终为1?

Any help will be very helpful for me. 任何帮助对我都会非常有帮助。

Thanks in advance 提前致谢

It is because your $collection after first line, will only contain 8 items and you are then passing the count of it as second argument to the LengthAwarePaginator - which will return the count of 8 items. 这是因为第一行之后的$collection仅包含8个项目,然后将其计数作为第二个参数传递给LengthAwarePaginatorLengthAwarePaginator将返回8个项目的计数。

What I would do in your case is the following: 在您的情况下,我将执行以下操作:

$all = $collection->sortBy('id');
$chunk = $collection->forPage(3, 8);

$paginator = new LengthAwarePaginator($chunk, $all->count(), 8, 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM