Har*_*Geo 3 laravel laravel-5 laravel-pagination
我正在尝试对数组数据进行分页,事实证明它比我想象的更具挑战性.
我正在使用Laravel 5
所以我有一个抽象的接口/存储库,我的所有其他模型都扩展到了,我在抽象存储库调用paginate中创建了一个方法.我把两者都包括在内
use Illuminate\Pagination\Paginator;
和
use Illuminate\Pagination\LengthAwarePaginator;
这是方法
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()));
}
Run Code Online (Sandbox Code Playgroud)
因此,您可以想象此函数接受$items一个$perPage变量数组,该变量指示要分页的项目数量以及$pageStart指示从哪个页面开始的项目.
分页工作,我可以LengthAwarePaginator在我做的时候看到实例dd(),所有这些值都很好.
当我显示结果时,问题就开始了.
当我这样做时{!! $instances->render() !!},paginator链接显示正常,page参数会根据链接而改变,但数据不会改变.每页的数据都相同.当我使用Eloquent时,Model::paginate(3)一切正常,但是当我dd()这个时,LengthAwarePaginator它与LengthAwarePaginator我的自定义分页器的实例完全相同,除了它对一个数组而不是一个集合进行分页.
sha*_*ddy 12
您没有通过当前页面,就像您应该这样,您也可以获得相同的数组.这会奏效
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()));
}
Run Code Online (Sandbox Code Playgroud)
如果您传递正确的值,您的功能也将起作用$pageStart-Request::get('page', 1)
| 归档时间: |
|
| 查看次数: |
8189 次 |
| 最近记录: |