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.
You can add the following code for Collection in the Providers/AppServiceProvider.
// Enable pagination
if (!Collection::hasMacro('paginate')) {
Collection::macro('paginate',
function ($perPage = 15, $page = null, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
return (new LengthAwarePaginator(
$this->forPage($page, $perPage)->values()->all(), $this->count(), $perPage, $page, $options))
->withPath('');
});
}
Then, you can call paginate from a Collection, just like an Eloquent model. For example
$pages = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->paginate(5);