所以我有一个部分,人们可以发表评论,并为这些评论点赞。我想根据它有多少个赞来组织评论。
所以我用了下面这样的东西
$art = Article::with('category')
->with(array('comments' => function($comments){
//i get the comments related to the article and the count of likes it has
$comments->with('likesCount');
}))
->find($id);这就是模型
<?php
class Comment extends Eloquent {
public function likes()
{
return $this->hasMany('CommentsLike','comment_id');
}
//here i take the count of likes, if i use ->count() it throw
public function likesCount()
{
return $this->likes()
->groupBy('comment_id')
->selectRaw('comment_id,count(*) as comment_likes');
}
}如何根据我在likesCount中获得的内容对我的评论进行排序
发布于 2017-06-16 14:33:52
使用orderBy() to likesCount()函数。
public function likesCount()
{
return $this->likes()
->groupBy('comment_id')
->selectRaw('comment_id,count(*) as comment_likes')
->orderBy('comment_likes', 'desc');
}https://stackoverflow.com/questions/44576382
复制相似问题