Laravel 5 Merge LengthAwarePaginator

时间:2017-08-03 06:19:51

标签: laravel-5 merge

我有两个不同查询的2个对象LengthAwarePaginator,我想要合并以显示在同一页面/表格上。

一旦合并了2个对象,我就失去了大部分属性,例如元素总数,并且无法使用Laravel 5 render()函数:

$ final = $ res_query_1->合并($ res_query_2)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

public function merge(LengthAwarePaginator $collection1, LengthAwarePaginator $collection2)
    {
        $total = $collection1->total() + $collection2->total();

        $perPage = $collection1->perPage() + $collection2->perPage();

        $items = array_merge($collection1->items(), $collection2->items());

        usort($items, array($this, 'cmp'));

        $paginator = new LengthAwarePaginator($items, $total, $perPage);

        return $paginator;
    }