Laravel 分页不适用于数组而不是集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30477915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel pagination not working with array instead of collection
提问by Harry Geo
I'm trying to paginate an array data set and it has proven more challenging than I thought.
我正在尝试对数组数据集进行分页,事实证明它比我想象的更具挑战性。
I'm using Laravel 5
我正在使用 Laravel 5
So I have an abstract interface/repository that all my other models extend to and I created a method inside my abstract repository call paginate. I've included both
所以我有一个抽象接口/存储库,我的所有其他模型都扩展到它,我在我的抽象存储库调用 paginate 中创建了一个方法。我已经包括了
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\Paginator;
and
和
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\LengthAwarePaginator;
Here is the method
这是方法
public function paginate($items,$perPage,$pageStart=1)
{
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
So as you can imagine this function accepts an array of $items
a $perPage
variable that indicates how many to items to paginate and a $pageStart
that indicates from which page to start.
因此,您可以想象此函数接受$items
一个$perPage
变量数组,该数组指示要分页的项目数和$pageStart
指示从哪个页面开始的变量。
The pagination works and I can see LengthAwarePaginator
instance when I'm doing a dd()
, all of it's values seem fine.
分页有效,我可以LengthAwarePaginator
在执行 a 时看到实例dd()
,它的所有值似乎都很好。
The problem starts when I'm displaying the results.
当我显示结果时,问题就开始了。
When I do {!! $instances->render() !!}
The paginator links display's fine, the page
parameter changes according to links but the data is not changing.
Data is the same in every page. When I'm using Eloquent for example Model::paginate(3)
everything works fine, but when I dd()
this LengthAwarePaginator
it's identical to the LengthAwarePaginator
instance of my custom paginator with the exception that it paginates an array ofcourse and not a collection.
当我做{!! $instances->render() !!}
分页器链接显示正常时,page
参数根据链接而变化,但数据没有变化。每一页的数据都是一样的。当我用雄辩如Model::paginate(3)
一切正常,但是当我dd()
这LengthAwarePaginator
是等同于LengthAwarePaginator
我与它进行分页ofcourse一个数组,而不是一个集合异常的自定义分页程序的实例。
回答by Sh1d0w
You are not passing the current page, like you should so you also get same array. This will work
你没有传递当前页面,就像你应该的那样,所以你也得到了相同的数组。这将工作
public function paginate($items,$perPage)
{
$pageStart = \Request::get('page', 1);
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
Your function will also work if you pass correct value for $pageStart
- Request::get('page', 1)
如果您传递正确的值,您的功能也将起作用$pageStart
-Request::get('page', 1)