【发布时间】:2019-09-03 05:38:03
【问题描述】:
我使用的是 laravel 5.7
我有 whereIn 子句的查询
在我的控制器中,我有这样的查询:
$post = Post::whereIn('reply_from_id', [1,2])
->orderBy('created_at', 'desc')->get();
我得到这样的输出:
[
{
"id": 3,
"title": "test reply id 2",
"reply_from_id": 2
},
{
"id": 4,
"title": "test reply id 1",
"reply_from_id": 1
},
{
"id": 5,
"title": "test reply id 1 again",
"reply_from_id": 1
},
{
"id": 6,
"title": "test reply id 1 again and again",
"reply_from_id": 1
},
{
"id": 7,
"title": "test reply id 2 again",
"reply_from_id": 2
}
]
我想限制输出每个 ID 只显示 2 个数据
例如:
[
{
"id": 3,
"title": "test reply id 2",
"reply_from_id": 2
},
{
"id": 4,
"title": "test reply id 1",
"reply_from_id": 1
},
{
"id": 5,
"title": "test reply id 1 again",
"reply_from_id": 1
},
{
"id": 7,
"title": "test reply id 2 again",
"reply_from_id": 2
},
]
我尝试像这样使用take():
$post = Post::whereIn('reply_from_id', [1,2])
->take(2)
->orderBy('created_at', 'desc')->get();
但这只是显示这样的数据
[
{
"id": 3,
"title": "test reply id 2",
"reply_from_id": 2
},
{
"id": 4,
"title": "test reply id 1",
"reply_from_id": 1
}
]
whereIn子句中如何限制数据?
【问题讨论】:
标签: php laravel postgresql eloquent