如何获得评论帖子的所有用户?

时间:2018-05-06 20:12:41

标签: php mysql laravel laravel-5

我正在使用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'
        );
    }
}

我的网络应用程序显示groups,您可以在其中创建posts以及在哪个帖子中撰写comments。在我的数据库中,我有以下关系:

  • 组:(id,name,description);
  • PostGroup :( id,group_id,user_id,post_content);
  • PostComment:(id,post_group_id,user_id,comment_content);

我想要做的是来创建用户对象的集合,然后进行查询以获取所有订阅了某个帖子的用户的用户< / strong>,在MySQL看起来像:

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'))->

所以,我想要做的是扩展该查询以获得->get()->sortBy('last_name');所需的结果,我怎样才能在whereIn之后将其排除?

修改

user.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');
    }
}

2 个答案:

答案 0 :(得分:1)

根据您的说明,您已经提前提供了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())

如果您还需要检查用户对某个组的有效订阅,则会稍微复杂一些。但是,正如您所评论的那样,您已经只找到了一个组的用户,所以这应该没问题。

答案 1 :(得分:0)

在PostComment中,试试这个

$this->selectRaw(‘user_id, comment_content’)->where(‘post_group_id’, 1)->groupBy(‘user_id’)->get();