Answer the question
In order to leave comments, you need to log in
How to get a list of all users who wrote a comment on an article?
How to get a list of all users who wrote a comment on an article?
Now implemented like this:
$list = Comment::where('article_id', 1)->get()->load('user');
$users = $list->map(function($list){
return $list->user;
})->unique();
Answer the question
In order to leave comments, you need to log in
First, the database structure:
Comment (article_id, id, text, user_id)
Article (id, text, user_id)
User (id, name)
public function comments(){
return $this->hasMany(Comment::class);
}
public function author(){
return $this->belongsTo(User::class);
}
public function commetators(){
return $this->hasManyThrough(User::class, Comment::class);
public function article(){
return $this->belongsTo(Article::class);
}
public function user(){
return $this->belongsTo(User::class);
}
$users = Article::find(1)->commentators;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question