问题描述
我正在创建一个流,其中包含两种类型的对象:BluePerson和RedPerson.为了创建流,我获取了所有两个对象,然后将它们合并为一个集合.这样做之后,我需要对它们进行分页,但是看来分页是针对雄辩的模型和数据库查询,而不是集合.
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);
}
推荐答案
你是对的.但是有一个用于集合的分页器功能. forPage
You are right. but there is ineed a paginator function for collections. forPage
Collection forPage(int $page, int $perPage)
示例:
休息很简单.
Example:
Rest is simple.
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中对合并的集合进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!