我的脑子突然在这个问题上崩溃了。任何愿意帮助我的人都非常感谢。
这是 Laravel 5.4 中的 LengthAwarepaginator
这是代码。
$collection = [];
foreach ($maincategories->merchantCategory as $merchantCat) {
foreach ($merchantCat->merchantSubcategory as $merchantSub) {
foreach($merchantSub->products as $products){
$collection[] = $products;
}
}
}
$paginate = new LengthAwarePaginator($collection, count($collection), 10, 1, ['path'=>url('api/products')]);
dd($paginate);
它显示完美,但问题是项目是 100。这是我所有的项目,我正确指定了它。我只需要显示 10。
基于 LengthAwarePaginator 构造函数。这是引用。
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
这是屏幕截图。
我哪里做错了?泰
最佳答案
手动创建分页器时,您必须自己对结果集进行切片。分页器的第一个参数应该是所需的结果页面,而不是整个结果集。
来自 pagination documentation :
When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the array_slice PHP function.
我建议使用
Collection
帮助解决这个问题:// ...
$collection = collect($collection);
$page = 1;
$perPage = 10;
$paginate = new LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page,
['path' => url('api/products')]
);
关于Laravel 5.4 LengthAwarePaginator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601095/