array_slice()期望参数1为数组 - 手动创建Paginator

时间:2018-04-18 09:18:15

标签: php laravel pagination

我正在尝试联合3个SQL查询,因此我可以将duedate命名为newDate并手动创建分页器。

但由于某种原因引发了这个例外:

  

array_slice()期望参数1为数组,给定对象

我的代码:

public function index(Request $request)
{
    $TMUDue = Equipments::Where('due1', '<>', '1990-01-01')
                            ->select('*', 'due1 AS newDate');
    $CalDue = Equipments::Where('due2', '<>', '1990-01-01')
                            ->select('*', 'due2 AS newDate');
    $SerDue = Equipments::Where('due3', '<>', '1990-01-01')
                            ->select('*', 'due3 AS newDate');

    $equipmentsDue = $SerDue->union($CalDue)->union($TMUDue)->orderBy('newDate')->get();

    $page = Input::get('page', 1);
    $paginate = 10;

    $offSet = ($page * $paginate) - $paginate;
    $itemsForCurrentPage = array_slice($equipmentsDue, $offSet, $paginate, true);
    $equipmentsDue = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($equipmentsDue), $paginate, $page);
    return view('dashboard.index', compact('equipmentsDue'));

我做错了吗?

如果不手动创建数据库分页,查询就可以正常工作。

1 个答案:

答案 0 :(得分:1)

它清楚地表明您需要传递数组而不是集合,因此请使用->toArray();

进行更改
$equipmentsDue = $SerDue->union($CalDue)
->union($TMUDue)->orderBy($sort , $sort2)->get()->toArray();