我想要用户之间的聊天计数,并按他们的聊天 ID 分组。但是查询没有给出聊天次数。它的空白。我在 Stack Overflow ( Laravel Eloquent groupBy() AND count , Laravel get count of results based on groupBy ) 中看到了类似的问题,并使用了那些标记为正确但仍未获得所需结果的查询。
我正在使用以下查询:
$user_info = DB::table('chat_messages')
->select(DB::raw('count(*) as total'))
->where('viewed','=', '0')
->groupBy('chat_id','type')
->get();
我的表结构如下:
{
"_id": ObjectId("571f549b1c8cb6972c8b4567"),
"message": "Testing Chat Functionality",
"type": "singlechat",
"date": "2016-04-26 11:44:27",
"from": "56b07a5a083f119a0b8b4569",
"to": "56b07a5a083f119a0b8b4569",
"chat_id": "56f65fc51c8cb6ca1a8b4567",
"status": "1",
"updated_at": ISODate("2016-04-26T11:44:27.624Z"),
"created_at": ISODate("2016-04-26T11:44:27.624Z"),
"viewed": "0"
}
结果是这样的:
Array
(
[0] => Array
(
[_id] => Array
(
[chat_id] => 56f65fc51c8cb6ca1a8b4567
[type] => singlechat
)
[chat_id] => 56f65fc51c8cb6ca1a8b4567
[type] => singlechat
[count(*) as total] =>
)
[1] => Array
(
[_id] => Array
(
[chat_id] => 56fa3f5f1c8cb667138b4567
[type] => groupchat
)
[chat_id] => 56fa3f5f1c8cb667138b4567
[type] => groupchat
[count(*) as total] =>
)
)
最佳答案
$user_info = DB::table('chat_messages')
->where('viewed','=', '0')
->select(DB::raw('count(chat_messages.*) as total'))
->groupBy('chat_id','type')
->get();
这应该可以,我现在无法测试
关于php - 使用 GroupBy 计算在 Laravel 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37134923/