Laravel 4:按评论数量 'Likes' 对评论进行排序
Laravel 4: Sorting comments by how many 'Likes' they have
我有两个 table:评论和 CommentRatings
我想做的是获取评论,但根据他们在 CommentRatings 中的点赞数对它们进行排序 table,但我只是想不通,我想在这里问我会有更多运气
这是我的评论 Table:
这是 CommentRatings Table:
理想情况下,我需要能够根据有多少喜欢来对它进行排序,但实际上没有任何线索
$comments = Comment::where('url', Request::url())
->take($limit)
->get()
我不积极开发 Laravel 4,但我试了一下。希望它能在 4 中工作。在 Laravel 5 中测试过,但如果这会引发任何错误,请告诉我。
$comments = Comment::where('url', Request::url())
->select(DB::raw('comments.*, count(*) as `comments_count`'))
->join('comment_ratings', 'comments.id', '=', 'comment_ratings.comment_id')
->groupBy('id')
->orderBy('comments_count', 'desc')
->take($limit);
我有两个 table:评论和 CommentRatings
我想做的是获取评论,但根据他们在 CommentRatings 中的点赞数对它们进行排序 table,但我只是想不通,我想在这里问我会有更多运气
这是我的评论 Table:
这是 CommentRatings Table:
理想情况下,我需要能够根据有多少喜欢来对它进行排序,但实际上没有任何线索
$comments = Comment::where('url', Request::url())
->take($limit)
->get()
我不积极开发 Laravel 4,但我试了一下。希望它能在 4 中工作。在 Laravel 5 中测试过,但如果这会引发任何错误,请告诉我。
$comments = Comment::where('url', Request::url())
->select(DB::raw('comments.*, count(*) as `comments_count`'))
->join('comment_ratings', 'comments.id', '=', 'comment_ratings.comment_id')
->groupBy('id')
->orderBy('comments_count', 'desc')
->take($limit);