繁体   English   中英

我如何通过Laravel中的流行线程选择表线程顺序

[英]How can i select table thread order by popular thread in laravel

当差异表中存在流行参数时,我真的很迷惑用于选择流行线程的雄辩,所以我的表看起来像下面这样:

-------------------
|    thread        |
-------------------
|id (int)          |
|title (varchar)   |
|value (text)      |
|timestamp         |
-------------------

 -------------------
|    comment       |
-------------------
|id (int)          |
|value (text)      |
|id_thread (id)    |
|timestamp         |
-------------------

因此,在上表中,我想使用雄辩的laravel按评论最多的线程显示该线程的顺序。 我花很多时间在这种情况下,但没有解决办法。

尝试

$tops = Thread::join('comments', 'comments.id_thread', '=', 'threads.id')
->select(
    'thread.*', 
    DB::raw('count(comments.id) as total_comments')
)->orderBy('total_comments', 'desc')
->groupBy('threads.id') // or 'id'
->take(10) //top 10 ?
->get();

您可能会遇到一个与GROUPBY相关的错误,说“ ...,因为它不包含在grouby中”。 这意味着您必须按该列分组。 显示该错误的每一列,将其添加到“分组依据”中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM