How can I paginate a merged collection in Laravel 5?(如何在 Laravel 5 中对合并的集合进行分页?)
问题描述
我正在创建一个流,其中包含两种类型的对象,BluePerson 和 RedPerson.为了创建流,我获取了所有两个对象,然后将它们合并到一个集合中.这样做之后,我需要对它们进行分页,但是 paginate 似乎是用于 eloquent 模型和数据库查询,而不是集合.我已经看到很多关于手动创建分页器的文档,尤其是在 API 中的文档很少(我什至找不到分页器类接受的参数.)
I am creating a stream which contains two types of objects, BluePerson and RedPerson. To create the stream, I fetch all of both objects, then merge them into one collection. After doing so, I need to paginate them, however paginate is for eloquent models and DB queries, and not collections, it seems. I have seen a lot about manually creating a paginator, but the documentation, especially in the API is sparse (I can't even seem to find the arguments the Paginator class accepts.)
如何对合并收藏的结果进行分页?
How can I paginate the results of merging my collections?
public function index()
{
$bluePerson = BluePerson::all();
$redPerson = RedPerson::all();
$people = $bluePerson->merge($redPerson)->sortByDesc('created_at');
return view('stream.index')->with('people', $people);
}
推荐答案
但是 paginate 似乎是用于 eloquent 模型和数据库查询,而不是集合.
however paginate is for eloquent models and DB queries, and not collections, it seems.
你说得对.但是需要一个用于集合的分页器功能.forPage
You are right. but there is ineed a paginator function for collections. forPage
Collection forPage(int $page, int $perPage)
示例:
休息很简单.
public function foo()
{
$collection = collect([1,2,3,4,5,6,7,8,9,0]);
$items = $collection->forPage($_GET['page'], 5); //Filter the page var
dd($items);
}
这篇关于如何在 Laravel 5 中对合并的集合进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Laravel 5 中对合并的集合进行分页?
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01