合并的Laravel分页()

时间:2015-09-12 14:33:35

标签: laravel laravel-5

我合并了2个收藏集:

use Illuminate\Pagination\LengthAwarePaginator as Paginator;

...

$towni18n = TownI18n::where(...)->get();
$town = Town::where(...)->get();
$all = $towni18n->merge($town);

$perPage = 10;
$input = \Input::all();
if (isset($input['page']) && !empty($input['page'])) { $currentPage = $input['page']; } else { $currentPage = 1; }

$paginator = new Paginator($all, $all->count(), $perPage, $currentPage);
return $paginator;

输出不跟随分页但返回所有结果:

{
total: 547,
per_page: 10,
current_page: 1,
last_page: 55,
next_page_url: "/?page=2",
prev_page_url: null,
from: 1,
to: 547,
data: [
{
id: 1423550,

...

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

似乎你必须手动切片。来自Laravel 5.1 doc:

  

“手动创建分页器实例时,您应该手动创建   “切片”传递给分页器的结果数组。“

所以答案是:

$arr = $all->toArray();
$offset = ($currentPage * $perPage) - $perPage;
$arr_splice = array_slice($arr, $offset, $perPage, true);
$paginator = new Paginator($arr_splice, count($arr), $perPage, $currentPage);