Laravel获得用户评论的所有帖子
问题描述:
我有3个名为 User
, Post
, Comment
的模型,并且这些模型相关.
I have 3 Model named User
, Post
, Comment
and this models are related.
用户模型:该模型具有许多Post模型.
User Model : This model has many Post model.
发布模型:该模型属于用户模型,并且具有许多注释模型.
Post Model : This model belongs to User model and has many Comment model.
评论模型:该模型属于发布模型和用户模型.
Comment Model : This model belongs to Post model and User model.
现在的问题是,如何获得用户评论的所有帖子?
Now the question is how can i get all posts that a user commented on?
答
$posts = Post::whereHas('comment', function($query) use($user) {
$query->whereUserId($user->id);
})->get();
$ user
是您感兴趣的用户.