Laravel 分页不适用于数组而不是集合
Laravel pagination not working with array instead of collection
我正在尝试对数组数据集进行分页,事实证明它比我想象的更具挑战性。
我正在使用 Laravel 5
所以我有一个抽象 interface/repository,我的所有其他模型都扩展到它,并且我在我的抽象存储库调用 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()));
}
所以你可以想象这个函数接受一个 $items
数组,一个 $perPage
变量指示要分页的项目数和一个 $pageStart
指示从哪个页面开始.
分页有效,当我执行 dd()
时,我可以看到 LengthAwarePaginator
实例,它的所有值似乎都很好。
显示结果时出现问题。
当我执行 {!! $instances->render() !!}
时,分页器链接显示正常,page
参数根据链接而变化,但数据没有变化。
每个页面的数据都是一样的。当我使用 Eloquent 例如 Model::paginate(3)
时一切正常,但是当我 dd()
这个 LengthAwarePaginator
它与我的自定义分页器的 LengthAwarePaginator
实例相同唯一的例外是它对一个数组 ofcourse 而不是一个集合进行分页。
您没有像您应该的那样传递当前页面,因此您也会得到相同的数组。这会起作用
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()));
}
如果您为 $pageStart
- Request::get('page', 1)
传递正确的值,您的函数也将起作用
我正在尝试对数组数据集进行分页,事实证明它比我想象的更具挑战性。
我正在使用 Laravel 5
所以我有一个抽象 interface/repository,我的所有其他模型都扩展到它,并且我在我的抽象存储库调用 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()));
}
所以你可以想象这个函数接受一个 $items
数组,一个 $perPage
变量指示要分页的项目数和一个 $pageStart
指示从哪个页面开始.
分页有效,当我执行 dd()
时,我可以看到 LengthAwarePaginator
实例,它的所有值似乎都很好。
显示结果时出现问题。
当我执行 {!! $instances->render() !!}
时,分页器链接显示正常,page
参数根据链接而变化,但数据没有变化。
每个页面的数据都是一样的。当我使用 Eloquent 例如 Model::paginate(3)
时一切正常,但是当我 dd()
这个 LengthAwarePaginator
它与我的自定义分页器的 LengthAwarePaginator
实例相同唯一的例外是它对一个数组 ofcourse 而不是一个集合进行分页。
您没有像您应该的那样传递当前页面,因此您也会得到相同的数组。这会起作用
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()));
}
如果您为 $pageStart
- Request::get('page', 1)