Php 如何对使用with()返回的行进行分页?

Php 如何对使用with()返回的行进行分页?,php,mysql,laravel,Php,Mysql,Laravel,我的问题是: $questions = Questions::with('answers')->where('id', $request->id)->get(); 它工作得很好,一切都很好。它匹配一个问题及其所有答案。我需要把答案分页,我怎么做 如您所见,我不想直接对查询进行分页(在这种情况下,我们可以使用->paginate()而不是->get())。但是当我使用with()并且我想用with()对中匹配的行进行分页时,我该怎么做呢?您可以使用 或者您可以将查询转过来: $

我的问题是:

$questions = Questions::with('answers')->where('id', $request->id)->get();
它工作得很好,一切都很好。它匹配一个问题及其所有答案。我需要把答案分页,我怎么做


如您所见,我不想直接对查询进行分页(在这种情况下,我们可以使用
->paginate()
而不是
->get()
)。但是当我使用
with()
并且我想用
with()
中匹配的行进行分页时,我该怎么做呢?

您可以使用

或者您可以将查询转过来:

$answers = Answer::whereHas('question', function($q) use ($request) {
    $q->where('id', $request->id);
})->with('question')->paginate();


但是您不能使用
->with()
方法进行分页。

另外,我想了解一下
Answer::whereHas('question',
)中的
问题是什么?
模型的名称?
Answer
模型中关系的名称。我刚才假设您可能没有定义它。
$question = Question::find($request->id);
$answers = $question->answers()->paginate();