Laravel有许多关系计数在帖子上的喜欢和评论数量

时间:2013-12-25 07:37:01

标签: php mysql laravel eloquent

代码:

$posts = Jumpsite::find($jid)
            ->posts()
            ->with('comments')
            ->with('likes')
            ->with('number_of_comments')
            ->with('number_of_likes')
            ->where('reply_to', 0)
            ->orderBy('pid', 'DESC')
            ->paginate(10);

每个帖子都有评论和喜欢。我最初只显示一些注释以避免大负荷。但是我希望显示每篇文章的评论和喜欢的数量。我该怎么做?

型号代码:

public function likes()
{
    return $this->hasMany('Like', 'pid', 'pid');
}

public function comments()
{
    return $this->hasMany('Post', 'reply_to', 'pid')->with('likes')->take(4);
}

public function number_of_likes()
{
    return $this->hasMany('Like', 'pid', 'pid')->count();
}

注意:

This is an API. All will be returned as JSON.

更新

回归

Post
    author_id
    message
    Comments(recent 4)
        user_id
        message
        post_date
        Number_of_likes
    Likes
        user_id
    Number_of_total_comments
    Number_of_total_likes

更新

我如何返回数据

$posts  = $posts->toArray();
$posts  = $posts['data'];

return Response::json(array(
   'data' => $posts
));

只需使用它,我就可以在json中获得我想要的所有数据。但我也想增加总数。


更新

protected $appends = array('total_likes');

public function getTotalLikesAttribute()
{
   return $this->hasMany('Like')->whereUserId($this->uid)->wherePostId($this->pid)->count();

}

但收到错误:

 Unknown column 'likes.post_id'

错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'likes.post_id' in 'where clause' (SQL: select count(*) as aggregate from `likes` where `likes`.`deleted_at` is null and `likes`.`post_id` = 4 and `pid` = 4 and `uid` = 1)

4 个答案:

答案 0 :(得分:7)

在您的模型中放置以下访问者:

统计总数:

 public function getTotalLikesAttribute()
 {
    return $this->hasMany('Like')->whereUserId($this->author_id)->count();

 }

计算总评论数:

根据您的描述,我可以看到,您已将帖子数量检索为评论

public function getTotalCommentsAttribute()
{
    return $this->hasMany('Post')->whereUserId($this->author_id)->count();    
}

现在,从您的控制器:

$post  = Jumpsite::find($jid);

// total comments
var_dump( $post->total_comments );

// total Likes
var_dump( $post->total_likes );

答案 1 :(得分:6)

您可以使用以下代码计算关系模型结果。

 $posts = App\Post::withCount('comments')->get(); foreach ($posts as $post) { echo $post->comments_count; }

并且还设置了像这样的计数条件

$posts = Post::withCount(['votes', 'comments' => function ($query) { $query->where('content', 'like', 'foo%'); }])->get();

答案 2 :(得分:2)

计算laravel中所有博客帖子的评论

第1步:将此代码放在“发布”模型中。

// Get the comments for the blog post.
public function comments()
{
    return $this->hasMany('App\Comment');
}

步骤2:将此代码放在“PostController”控制器中。

 $posts= Post::all();
 return view('home')->with('posts', $posts);

步骤3:然后使用以下代码计算所有帖子的评论。

@foreach($posts as $row)
 {{$row->comments->count()}}
@endforeach

请在此处查看详细信息:http://www.pranms.com/count-comments-for-all-blog-posts-in-laravel/

答案 3 :(得分:0)

只需单次更改即可获得计数结果

关系

 public function getTotalLikesAttribute(){
    return $this->hasMany('Like')->where('author_id',$this->author_id)->count();
}

在视图中

$post->getTotalLikesAttribute()