[英]How to show approved comments in Laravel
I want to show post's comments ( approved = true
) We have a single post with a random slug/id, But the problem is: How can I show approved comments with that specific post id !? 我想显示帖子的评论( approved = true
),我们有一个帖子,带有一个随机条目/ id,但是问题是:我如何显示具有特定帖子ID的已批准评论!
I think i have to use something like this following example : 我认为我必须使用类似以下示例的内容:
Post::find($id)->Comments()->whereApproved(true)->get()
Comments Table : 评论表:
As I understand from the description, approved
column is in the comments
table. 根据我的描述, approved
列在comments
表中。 You can get comments with just one query since you know post ID: 由于您知道帖子ID,因此只需一个查询就可以获取评论:
Comment::where('post_id', $postId)
->where('approved', true)
->get();
If you want to get the post with approved comments, do this: 如果您想获得带有批准评论的帖子,请执行以下操作:
Post::with(['comments' => function ($q) {
$q->where('approved', true);
}])->find($postId);
If approved is a column in comments table you should use this : 如果批注表中的一栏为已批准,则应使用以下代码:
Post::with(['comments' => function ($query) {
$query->where('approved', true);
}])->get();
This code returns all posts with approved comments 此代码返回所有带有批准评论的帖子
So you can use it in your blade 所以你可以在刀片中使用它
$post->Comments
Don't forget to use foreach
or looping 不要忘记使用foreach
或循环
$comments = Comments::where('comments',1)->get(); $ comments =评论:: where('comments',1)-> get(); here 1 is your approved comments 这里1是您的批准评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.