Laravel eloquent 按预加载分组排序
Laravel eloquent order by group by with eager loading
我有一个查询,它运行 group by 和 order by 预加载,看起来 group by 工作正常但 order by 不起作用。
Message::whereIn('conversation_id',$msgs)
->where('deleted', '!=', $userId)
->with(array('last_sender' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->with(array('last_reciever' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->orderBy('id', 'desc')
->orderBy('conversation_id', 'desc')
->groupBy('id')
->groupBy('conversation_id')
->get();
它 return 的数据是这样的
{
"data": [
{
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
},
{
"id": 132,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "second message to first user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:38:16",
"updated_at": "2017-10-17 15:38:16"
},
{
"id": 131,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message to second user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:37:49",
"updated_at": "2017-10-17 15:37:49"
},
{
"id": 130,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "Hi how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:36:49",
"updated_at": "2017-10-17 15:36:49"
}
]
}
但我只想return前两个最新结果。只有 id 133
和 132
我知道我添加了 ->groupBy('id')
这在这里没用,并且 return 了所有记录。我添加它只是为了显示我的数据是什么。所以如果我删除 ->groupBy('id')
那么它只有 returns id 131
和 130
请帮忙。谢谢你。
好的,我添加了一行就解决了:)
我正在发布其他人的帮助。
->orderBy('created_at', 'desc')
->get()
->unique('conversation_id');
我有一个查询,它运行 group by 和 order by 预加载,看起来 group by 工作正常但 order by 不起作用。
Message::whereIn('conversation_id',$msgs)
->where('deleted', '!=', $userId)
->with(array('last_sender' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->with(array('last_reciever' =>function($query) use ($userId){
$query->where('id','!=', $userId);
$query->select('id', 'userName', 'profilePic','firstName', 'lastName');
}))
->orderBy('id', 'desc')
->orderBy('conversation_id', 'desc')
->groupBy('id')
->groupBy('conversation_id')
->get();
它 return 的数据是这样的
{
"data": [
{
"id": 133,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message 2 to user second",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:43:14",
"updated_at": "2017-10-17 15:43:14"
},
{
"id": 132,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "second message to first user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:38:16",
"updated_at": "2017-10-17 15:38:16"
},
{
"id": 131,
"conversation_id": 13,
"last_sender": null,
"last_reciever": {
"id": 55,
"userName": "buyer",
"profilePic": "pRBDBFJW55baSnF560Ajid8jTgPo5kmg4i5LMhPG.jpeg",
"firstName": "Matti",
"lastName": "Rames"
},
"msg": "second message to second user",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:37:49",
"updated_at": "2017-10-17 15:37:49"
},
{
"id": 130,
"conversation_id": 11,
"last_sender": null,
"last_reciever": {
"id": 54,
"userName": "Sadek",
"profilePic": "Nir7zgorNT2dmwcXJdhNK3ZmPAltmkEnj0SXDCDC.png",
"firstName": "Sadek",
"lastName": "Hossain"
},
"msg": "Hi how are you",
"attachment": null,
"deleted": 0,
"seen": 0,
"created_at": "2017-10-17 15:36:49",
"updated_at": "2017-10-17 15:36:49"
}
]
}
但我只想return前两个最新结果。只有 id 133
和 132
我知道我添加了 ->groupBy('id')
这在这里没用,并且 return 了所有记录。我添加它只是为了显示我的数据是什么。所以如果我删除 ->groupBy('id')
那么它只有 returns id 131
和 130
请帮忙。谢谢你。
好的,我添加了一行就解决了:) 我正在发布其他人的帮助。
->orderBy('created_at', 'desc')
->get()
->unique('conversation_id');