Laravel hasMany 关系统计帖子的点赞数和评论数
本文介绍了Laravel hasMany 关系统计帖子的点赞数和评论数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
每个帖子都有评论和喜欢.我最初只显示一些评论以避免大量加载.但我想显示每个帖子的总评论数和点赞数.我该怎么做?
Each post has a comment and likes. I only display a few of the comments initially to avoid large loads. But I want to show how many the total comments and likes for each post. How do I do this?
型号代码:
注意:
<小时>
更新
回归
<小时>
更新
我如何返回数据
只要使用它,我就可以在 json 中获取我想要的所有数据.但我也想加上总数.
Just by using that I get all the data i want in the json. But I also want to add the total counts.
更新
但得到错误:
<小时>
错误
推荐答案
在您的模型中放置以下访问器:
In your model place the following accessors:
点赞总数:
统计评论总数:
从你的描述中,我可以看到,你已经检索了作为评论的帖子数量
From your description, i can see, you have retrieving the number of posts as comments
现在,从您的控制器:
这篇关于Laravel hasMany 关系统计帖子的点赞数和评论数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!