Php 我怎样才能得到所有评论过帖子的用户?
我与拉威尔5号合作,我有以下模型 PostComment.phpPhp 我怎样才能得到所有评论过帖子的用户?,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我与拉威尔5号合作,我有以下模型 PostComment.php class PostComment extends Model { protected $fillable = [ 'post_group_id', 'user_id', 'comment_content' ]; public function post(){ return $this->belongsTo('App\PostGroup'); } p
class PostComment extends Model
{
protected $fillable = [
'post_group_id', 'user_id', 'comment_content'
];
public function post(){
return $this->belongsTo('App\PostGroup');
}
public function user(){
return $this->belongsTo('App\User');
}
}
PostGroup.php
class PostGroup extends Model
{
protected $fillable = [
'group_id', 'user_id', 'post_content'
];
public function user(){
return $this->belongsTo('App\User');
}
public function group(){
return $this->belongsTo('App\Group');
}
public function commented(){
return $this->hasMany(
'App\PostComment'
);
}
}
Group.php
class Group extends Model
{
protected $fillable = ([
'id'
]);
public function users(){
return $this->belongsToMany(
'App\User',
'user_group'
);
}
public function members(){
return $this->belongsToMany(
'App\User',
'user_group'
)->wherePivot('state','accepted');
}
public function posted(){
return $this->hasMany(
'App\PostGroup'
);
}
}
我的web应用程序显示组
,您可以在其中创建帖子
,也可以在其中撰写评论
。在我的数据库中,我有以下关系:
- 组:(id、名称、描述)李>
- PostGroup:(id,group\u id,user\u id,post\u内容)李>
- 帖子:(id、帖子组id、用户id、评论内容)李>
select users.* from users, post_comments where users.id = post_comments.user_id and post_comments.post_group_id="1"
在我的控制器中,我有以下代码
$theGroup = Group::find($groupId);
$thePost = PostGroup::find($postId);
$memberList = User::where('id', '<>', Auth::user()->id)->whereIn('id', $theGroup->users->pluck('id'))->
根据您的描述,您已经提前列出了
用户的列表
,因此您只能找到这些特定用户的帖子
基本上,您需要使用whereHas($relation,$calback)
执行您描述的检查:
$userIds = [2, 3, 5, 7, 11, 13, 17]; // or query them...
$postId = 123; // the id of the post where we want to look through the comments
User::where('id', '<>', Auth::id())
->whereIn('id', $userIds)
->whereHas('comments', function ($query) use ($postId) {
$query->where('post_group_id', $postId);
})
->get();
$userIds=[2,3,5,7,11,13,17];//或者询问他们。。。
$postId=123;//我们要在其中查看评论的帖子的id
User::where('id','',Auth::id())
->其中('id',$userid)
->whereHas('comments',function($query)use($posted){
$query->where('post\u group\u id',$posted);
})
->get();
这将简单地检查用户是否为给定的帖子编写了评论。因为您忘记发布您的用户
模型,所以我假设用户的注释
有一个可用的关系
如果需要,还可以将前两个条件(列表中的用户,但不是经过身份验证的用户)组合成一个<代码>$userIds=array_diff($userId,[Auth::id()])
执行此任务<代码>其中('id','',Auth::id())可以从查询中删除
如果您还需要检查用户对某个组的活动订阅,这将稍微复杂一些。但是,正如您所评论的,您已经为一个组找到了唯一的用户,所以这应该没问题。在PostComment中,试试这个
$this->selectRaw(‘user_id, comment_content’)->where(‘post_group_id’, 1)->groupBy(‘user_id’)->get();
用户是否有可能对一篇文章发表了评论(这意味着他是撰写该文章的小组的一部分),但不再是小组的一部分?如果是,用户是否应该出现在帖子的结果中?否,因为在查询的第一部分
user::where('id','',Auth::user()->->id)->其中('id',$theGroup->users->pulk('id'))
我将所有用户都放在一个组中,因此考虑这个问题没有问题,因为用户将其从组中排除,对我来说没关系。我希望这是清楚的。我总是需要订阅组的用户,但只需要对某个帖子发表评论的用户,这是因为我要创建一个通知系统,并且只针对成员。正如我所说,用户列表如下:$memberList=User::where('id','',Auth::User()->id)->其中('id',$theGroup->users->pull('id'))->get()
,该查询将向我提供组中的所有成员,我只需要处理此用户列表上的查询,我将尝试您的解决方案并让您知道。如果需要,我将发布用户模型。我获取local。错误:调用未定义的方法illumb\Database\query\Builder::comments()
Swapwhere has('comments'
用于whereHas('commented'
),您应该很好。
$this->selectRaw(‘user_id, comment_content’)->where(‘post_group_id’, 1)->groupBy(‘user_id’)->get();