Laravel已经有很多关系数,喜欢和评论的帖子 [英] Laravel hasMany relation count number of likes and comments on post
问题描述
代码:
$ posts = Jumpsite :: find($ jid)
- > )
- > with('comments')
- > with('likes')
- > with('number_of_comments')
- > with('
- > where('reply_to',0)
- > orderBy('pid','DESC')
- > paginate(10);
每篇文章都有评论,喜欢。我最初只显示一些评论,以避免大量负载。但是我想要显示每个帖子的总评论和喜欢的数量。如何做?
型号代码:
public function喜欢()
{
return $ this-> hasMany('Like','pid','pid');
}
public function comments()
{
return $ this-> hasMany('Post','reply_to','pid') - >用( '喜欢') - >取(4);
}
public function number_of_likes()
{
return $ this-> hasMany('Like','pid','pid') - >计数();
}
注意:
这是一个API。所有的都将返回为JSON。
更新 p>
返回
发布
author_id
message
评论(最近4)
user_id
消息
post_date
Number_of_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 ($这 - > PID) - >计数();
}
但收到错误:
未知列'likes.post_id'
错误
SQLSTATE [42S22] :未找到列:1054'where子句'中的未知列'likes.post_id'(SQL:select count(*)as aggregate from`likes`,其中`likes`.`deleted_at`为null和`likes`.`post_id` = 4和`pid` = 4和`uid` = 1)
在您的模型中,以下访问者:
计数总赞:
public function getTotalLikesAttribute()
{
return $ this-> hasMany('Like') - > whereUserId($ this-> author_id) - >计数();
}
计数总评论 / p>
从您的描述中,我可以看到,您已经检索了帖子的数量作为评论
public function getTotalCommentsAttribute()
{
return $ this-> hasMany('Post') - > whereUserId($ this-> author_id) - > count();
}
现在,从你的控制器:
$ post = Jumpsite :: find($ jid);
//总评论
var_dump($ post-> total_comments);
//总喜欢
var_dump($ post-> total_likes);
The code:
$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);
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?
Model code:
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();
}
Note:
This is an API. All will be returned as JSON.
update
The return
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
update
How I return the data
$posts = $posts->toArray();
$posts = $posts['data'];
return Response::json(array(
'data' => $posts
));
Just by using that I get all the data i want in the json. But I also want to add the total counts.
update
protected $appends = array('total_likes');
public function getTotalLikesAttribute()
{
return $this->hasMany('Like')->whereUserId($this->uid)->wherePostId($this->pid)->count();
}
but getting the error:
Unknown column 'likes.post_id'
error
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)
In your model place the following accessors:
Count total Likes:
public function getTotalLikesAttribute()
{
return $this->hasMany('Like')->whereUserId($this->author_id)->count();
}
Count total comments:
From your description, i can see, you have retrieving the number of posts as comments
public function getTotalCommentsAttribute()
{
return $this->hasMany('Post')->whereUserId($this->author_id)->count();
}
Now, from your controller:
$post = Jumpsite::find($jid);
// total comments
var_dump( $post->total_comments );
// total Likes
var_dump( $post->total_likes );
这篇关于Laravel已经有很多关系数,喜欢和评论的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!