我正在使用 Laravel 5,并且有以下模型
PostComment.php
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'
);
}
}
我的网络应用程序提供了组
,您可以在其中创建帖子
,并且可以在其中撰写评论
。在我的数据库中,我有以下关系:
- 群组:(ID、名称、描述);
- 帖子组:(id、group_id、user_id、post_content);
- 发表评论:(id、post_group_id、user_id、comment_content);
我想要做的是创建 User 对象的集合,然后进行查询以获取订阅某个群组并对某个帖子发表评论的所有用户,在 MySQL 中看起来像:
select users.* from users, post_comments where users.id = post_comments.user_id and post_comments.post_group_id="1"
所以在我的 Controller 中我有以下代码
$theGroup = Group::find($groupId);
$thePost = PostGroup::find($postId);
$memberList = User::where('id', '<>', Auth::user()->id)->whereIn('id', $theGroup->users->pluck('id'))->
所以,我想做的是扩展该查询以使用 ->get()->sortBy('last_name');
获得所需的结果,如何在在哪里
?
编辑
用户.php
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token','created_at','updated_at'
];
public function groups(){
return $this->belongsToMany('App\Group','user_group');
}
public function groupsAsAdmin(){
return $this->belongsToMany('App\Group','user_group')->wherePivot('role','admin');
}
public function groupsAsMember(){
return $this->belongsToMany('App\Group','user_group')->wherePivot('state','accepted');
}
public function groupsAsInvited(){
return $this->belongsToMany('App\Group','user_group')->wherePivot('state','pending');
}
public function posted(){
return $this->hasMany('App\PostGroup');
}
public function commented(){
return $this->hasMany('App\PostComment');
}
}
最佳答案
根据您的描述,您已经列出了 Users
的列表。提前,这样你只会找到Posts
与 Comment
这些特定用户。
基本上,您想要的是使用 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();
这将简单地检查用户是否编写了 Comment
对于给定的职位。因为您忘记发布您的User
模型,我假设有一个关系可用于 comments
用户的。
如果需要,您还可以将前两个条件(列表中的用户,但不是经过身份验证的用户)合并为一个。 $userIds = array_diff($userId, [Auth::id()])
做这个工作。 where('id', '<>', Auth::id())
然后可以从查询中删除。
如果您确实还需要检查用户对组的有效订阅,则情况会稍微复杂一些。但正如您所评论的,您已经只找到了一个组的用户,所以这应该没问题。
关于php - 如何获取所有评论过帖子的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50204103/