Laravel 5.4 - 如何对预加载关系进行分页
Laravel 5.4 - How to paginate eager loading relationships
我有以下代码来检索给定部分的线程及其评论和点赞。它有效,但我想对结果进行分页。
return $this->threads()->where('state', '=', 'active')->with('comments.likes')->get();
一种方法是执行此操作,但由于它不急于加载,因此会导致大量查询。
return $this->threads()->where('state', '=', active)->paginate(5);
有什么方法可以让我急切加载所有数据并利用 Laravel 的分页魔法?
您可以像这样对线程进行分页:
Thread::where('section_id', $sectionId)
->where('state', 'active')
->with('comments.likes')
->paginate(5);
我有以下代码来检索给定部分的线程及其评论和点赞。它有效,但我想对结果进行分页。
return $this->threads()->where('state', '=', 'active')->with('comments.likes')->get();
一种方法是执行此操作,但由于它不急于加载,因此会导致大量查询。
return $this->threads()->where('state', '=', active)->paginate(5);
有什么方法可以让我急切加载所有数据并利用 Laravel 的分页魔法?
您可以像这样对线程进行分页:
Thread::where('section_id', $sectionId)
->where('state', 'active')
->with('comments.likes')
->paginate(5);