【发布时间】:2020-11-25 15:28:21
【问题描述】:
这里是分页查询:
User::where('username', 'test')
->with('followers.follower_details')
->paginate(25)
但我想要在分页之前提取数据,如下所示:
User::where('username', 'test')
->with('followers.follower_details')
->pluck('followers')
->paginate(25)
通过谷歌搜索找不到任何有用的东西,有什么技巧可以处理这些类型的查询吗?
更新 我尝试了 KurtFriars 的工作方式,但数据的问题是
Follower::where('following',
User::query()->where('username', request()->username)
->firstOrFail()->id)->with('follower_details')->paginate(25)
回复是:
{
"current_page": 1,
"data": [
{
"id": 1,
"user_id": 1,
"following": 2,
"created_at": null,
"updated_at": null,
"follower_details": {
"name": "test test2",
"username": "test",
"email": "test@gmail.com"
}
},
{
"id": 5,
"user_id": 3,
"following": 2,
"created_at": null,
"updated_at": null,
"follower_details": {
"name": "test2",
"username": "test2",
"email": "test2@gmail.com",
}
}
],
.... // paginations links
}
但我只需要follower_details,像这样:
{
"current_page": 1,
"data": [
{
"name": "test test2",
"username": "test",
"email": "test@gmail.com"
},
{
"name": "test2",
"username": "test2",
"email": "test2@gmail.com",
}
],
"first_page_url": "http://localhost/api/user/nod/followers?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost/api/user/nod/followers?page=1",
"next_page_url": null,
"path": "http://localhost/api/user/nod/followers",
"per_page": 5,
"prev_page_url": null,
"to": 2,
"total": 2
}
【问题讨论】:
-
为什么要在分页前采摘?还有一件事,如果您不需要
follower_details,那您为什么要急切地加载它? -
@BasheerKharoti 因为我不需要属于
followers、followers.follower_details的数据,并且为了简单起见 -
所以你只需要
follower_details数据? -
@BasheerKharoti 是的
标签: laravel eloquent relationship