当流行参数存在于差异表中时,我真的很困惑用于选择流行线程的 Eloquent ,所以我的表如下所示:
-------------------
| thread |
-------------------
|id (int) |
|title (varchar) |
|value (text) |
|timestamp |
-------------------
-------------------
| comment |
-------------------
|id (int) |
|value (text) |
|id_thread (id) |
|timestamp |
-------------------
因此,在上面的表格中,我想使用 eloquent 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 中”。这意味着您必须按该列进行分组。显示该错误的每一列,将其添加到分组依据中。
关于php - 我如何在 laravel 中按流行线程选择表线程顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42660965/