Laravel 雄辩的分页两个关系合并

Laravel 雄辩的分页两个关系合并,laravel,pagination,eloquent,relationship,Laravel,Pagination,Eloquent,Relationship,我有两个模型:任务和注释 在我的用户档案中,我想按创建日期显示任务和注释排序 为此,我愿意: $timeline_array = $customer->comments; $timeline_array = $timeline_array->toBase()->merge($customer->tasks); //sort timeline event $timeline_array = $timeline_array->sortByDesc(function($

我有两个模型:任务和注释

在我的用户档案中,我想按创建日期显示任务和注释排序

为此,我愿意:

$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);

//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
    return $timeline_event->created_at;
});
我在我的视野中看到了我的阵列。它的工作很好,但如果我有太多的意见或任务,这将是一个大的要求,所以我想添加一个分页器

我怎么做

如果我有一个
$timeline\u数组->分页(5)最后我得到了错误:

方法Illumb\Support\Collection::paginate不存在

我认为这并不能解决我的问题,因为我在分页之前加载了所有的评论和任务


有人有想法/解决方案吗?

Paginate方法只适用于查询生成器或有说服力的, 有人在这里创建了一个要点,您可以在数组或集合上使用paginate:

试着像这样使用它:

$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);

//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
  return $timeline_event->created_at;
});

$timeline_array = $this->paginate($items);


public function paginate($items, $perPage = 15, $page = null, $options = [])
{
  $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
  $items = $items instanceof Collection ? $items : Collection::make($items);
  return new LengthAwarePaginator($items->forPage($page, $perPage), 
  $items->count(), $perPage, $page, $options);
}

或者将其作为宏添加到应用程序服务提供商的集合中,以获得更好的实践。

Paginate方法仅适用于查询生成器或雄辩的, 有人在这里创建了一个要点,您可以在数组或集合上使用paginate:

试着像这样使用它:

$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);

//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
  return $timeline_event->created_at;
});

$timeline_array = $this->paginate($items);


public function paginate($items, $perPage = 15, $page = null, $options = [])
{
  $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
  $items = $items instanceof Collection ? $items : Collection::make($items);
  return new LengthAwarePaginator($items->forPage($page, $perPage), 
  $items->count(), $perPage, $page, $options);
}

或者将其作为宏添加到应用程序服务提供商的集合中,以获得更好的实践。

最终找到了一个解决方案:

$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);

//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
  return $timeline_event->created_at;
});

$item_per_page = 10;
$timeline_array = new LengthAwarePaginator($timeline_array->forPage(Paginator::resolveCurrentPage(), $item_per_page), count($timeline_array), $item_per_page, Paginator::resolveCurrentPage(), [
  'path' => Paginator::resolveCurrentPath()
]);

最终找到了一个解决方案:

$timeline_array = $customer->comments;
$timeline_array = $timeline_array->toBase()->merge($customer->tasks);

//sort timeline event
$timeline_array = $timeline_array->sortByDesc(function($timeline_event){
  return $timeline_event->created_at;
});

$item_per_page = 10;
$timeline_array = new LengthAwarePaginator($timeline_array->forPage(Paginator::resolveCurrentPage(), $item_per_page), count($timeline_array), $item_per_page, Paginator::resolveCurrentPage(), [
  'path' => Paginator::resolveCurrentPath()
]);

这是正确的方法,但我需要使用
lengshawarepaginator
使它与我的项目一起工作,但谢谢!这是正确的方法,但我需要使用
lengshawarepaginator
使它与我的项目一起工作,但谢谢!