我有一个雄辩的要求接我的类别和相关的问题。
我希望questions部分有一个分页。
public function index()
{
$data = Category::where([
['slug', '=', 'interview-questions'],
['lang', '=', auth()->user()->lang],
['profile_id', '=', auth()->user()->profile_id]
])->with(
[
'questions' => function ($query) {
$query->whereNull('parent_id')->with(
[
'answer' => function ($query) {
$query->where('user_id', auth()->user()->id);
},
]
)->orderBy('position', 'ASC')->paginate(15);
}
]
)->first();
}我得到的是:
data: {id: 31, title: "Interview - Questions", slug: "interview-questions", part: "interview-questions",…}
created_at: "2019-08-22 16:28:33"
deleted_at: null
id: 31
lang: "fr"
part: "interview-questions"
profile_id: 1
questions: [{id: 956, question: "<h3>1. Qu’est-ce qui vous a intéressé dans notre annonce ? </h3>",…},…]
0: {id: 956, question: "<h3>1. Qu’est-ce qui vous a intéressé dans notre annonce ? </h3>",…}
1: {id: 957,…}
2: {id: 958,…}
3: {id: 959, question: "<h3>4. Depuis combien de temps cherchez-vous du travail ?</h3>",…}
4: {id: 960,…}
5: {id: 961,…}
6: {id: 962,…}
7: {id: 963,…}
8: {id: 964, question: "<h3>9. Pourquoi avez-vous postulé chez nous (candidature spontanée) ?</h3>",…}
9: {id: 965,…}
10: {id: 966, question: "<h3>11. Racontez-moi vos expériences professionnelles.</h3>", type: "wysiwyg",…} 我的问题限制在10个。只是,我没有像Laravel这样的分页信息,应该提供first_page_url、per_page、total、next_page_url等…。
为什么?如何解决这个问题?
发布于 2019-08-23 11:02:27
所以你想把你的关系分页,如果你有好的模型,我想你可以这样做:
public function index()
{
$data = Category::where([
['slug', '=', 'interview-questions'],
['lang', '=', auth()->user()->lang],
['profile_id', '=', auth()->user()->profile_id]
])->with(
[
'questions' => function ($query) {
$query->whereNull('parent_id')->with(
[
'answer' => function ($query) {
$query->where('user_id', auth()->user()->id);
},
]
)->orderBy('position', 'ASC');
}
]
)->first();
$data = $data->questions()->paginate(15);
}或者,您可以将关系数据单独分页并将其作为新变量发送:
$questions = $data->questions()->paginate(15);如果您有任何错误,请在这里提供。
祝好运!
发布于 2019-08-23 10:38:19
这是因为您在错误的地方使用分页,也使用first() -它只从过滤器获得第一个数据。将代码更改为:
public function index(){
$data = Category::where([
['slug', '=', 'interview-questions'],
['lang', '=', auth()->user()->lang],
['profile_id', '=', auth()->user()->profile_id]
])->with(
[
'questions' => function ($query) {
$query->whereNull('parent_id')->with(
[
'answer' => function ($query) {
$query->where('user_id', auth()->user()->id);
},
]
)->orderBy('position', 'ASC');
}
]
)->paginate(15);
}没有测试,但应该给你想要的结果。如果你有什么错误请告诉我。
发布于 2019-08-23 10:54:01
不能使用将分页本身作为分页的first()或get()方法,获取作为参数(限制)给出的结果数。对于您的情况,它将获取前15个结果并相应地分页。在访问其他页面时,它只是添加带有限制的偏移量。
下面是一个示例代码。注:分页将应用于类别,而不是问题。
public function index(){
$data = Category::where([
['slug', '=', 'interview-questions'],
['lang', '=', auth()->user()->lang],
['profile_id', '=', auth()->user()->profile_id]
])->with([
'questions' => function ($query) {
$query->whereNull('parent_id')->with([
'answer' => function ($query) {
$query->where('user_id', auth()->user()->id);
},
])->orderBy('position', 'ASC');
}
])->paginate(15);
}https://stackoverflow.com/questions/57624514
复制相似问题