Laravel 5.4 - 如何对热切的加载关系进行分页

时间:2017-04-22 18:11:29

标签: laravel pagination relationship eager-loading

我有以下代码来检索给定部分的线程及其注释及其喜好。它有效,但我想对结果进行分页。

return $this->threads()->where('state', '=', 'active')->with('comments.likes')->get();

一种方法是这样做,但它会产生大量的查询,因为它并不急于加载。

return $this->threads()->where('state', '=', active)->paginate(5);

有什么方法可以加载所有数据并利用Laravel的分页魔法?

1 个答案:

答案 0 :(得分:2)

你可以像这样分页线程:

Thread::where('section_id', $sectionId)
      ->where('state', 'active')
      ->with('comments.likes')
      ->paginate(5);
相关问题