0
我正尝试使用groupBy与Laravel的功能。这是我的查询是否有可能在laravel雄辩中使用groupBy与“with”功能
$msgs=Conversation::where('sender', $sender)
->orWhere('reciever', $sender)
->orderBy('created_at', 'desc')
->with('message')
->get();
此查询返回来自消息表的许多记录,但我只想检索一个。
这是数据,我想从组消息对象
{
"data": [
{
"id": 27,
"sender": 10,
"reciever": 4,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04",
"message": [
{
"id": 5,
"conversation_id": 27,
"last_sender": 10,
"msg": "5",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:04",
"updated_at": "2017-07-26 22:46:04"
},
{
"id": 6,
"conversation_id": 27,
"last_sender": 10,
"msg": "6",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:19",
"updated_at": "2017-07-26 22:46:19"
},
{
"id": 7,
"conversation_id": 27,
"last_sender": 10,
"msg": "7",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:35",
"updated_at": "2017-07-26 22:46:35"
},
{
"id": 8,
"conversation_id": 27,
"last_sender": 10,
"msg": "8",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:46:36",
"updated_at": "2017-07-26 22:46:36"
}
]
},
{
"id": 26,
"sender": 10,
"reciever": 7,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02",
"message": [
{
"id": 1,
"conversation_id": 26,
"last_sender": 10,
"msg": "1",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:40:02",
"updated_at": "2017-07-26 22:40:02"
},
{
"id": 2,
"conversation_id": 26,
"last_sender": 10,
"msg": "2",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:36",
"updated_at": "2017-07-26 22:45:36"
},
{
"id": 3,
"conversation_id": 26,
"last_sender": 10,
"msg": "3",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:50",
"updated_at": "2017-07-26 22:45:50"
},
{
"id": 4,
"conversation_id": 26,
"last_sender": 10,
"msg": "4",
"deleted": null,
"seen": null,
"created_at": "2017-07-26 22:45:57",
"updated_at": "2017-07-26 22:45:57"
}
]
}
]
}
任何帮助高度赞赏的结果。如果事情不清楚,请告诉我。谢谢。
谢谢你的帮助。但这不是我想要的。此查询限制为1,但我想对结果进行分组。你会更好地理解,如果你看到查询“string(93)”select * from'conversations'其中'sender' =?或'reciever' =? order by'created_at' desc“ string(104)”select * from'messages' where'messages'.'conversation_id' in(?,?)order by'created_at' desc limit 1“ string(104)”select * from'messages' where'messages'.'conversation_id' in(?,?)order by'created_at' desc limit 1“” – sadek
我猜我知道了,所以我说你可以在回调里面应用任何东西,你可以做如下: ' - > with(['message'=> function($ query){0}}返回$ query-> whereIn('conversation_id',['value1','value2']) - > orderBy('created_at' ,'desc') - > first();' }]) – Sletheren
再次感谢。但没有运气。这是我用过的 “Conversation :: where('sender',$ sender) - > orWhere('reciever',$ sender) - > orderBy('created_at','desc') - > with([ 'message'=> function($ query){ $ query-> whereIn('conversation_id',['msg','seen']); }]) - > get();“ 它不给任何消息对象的值.. 有一件事我注意到,我几乎可以使用任何东西,但不能在回调中使用groupBy。 – sadek