How to build the relationship between the three tables in laravel?

0 like 0 dislike
30 views
There are 3 tables: articles, comments, users. This relationship - the article can have many comments:
public function comments() { return $this->hasMany('App\\Comments'); }


And each comment has 1 user. (model users)
public function comments() { return $this->hasMany('App\\post', 'user_id'); }


How to make so that in the sample of comments to a specific article was all the data from the users table? This code selects all of the comments to the article:
$comments = Posts::where('id', $news->id)->first()->comments;


That it is necessary to ascribe to in $comments in addition to comments was the info from the users table? Ie I want to make an analogue of the join means eloquent.
asked by | 30 views

2 Answers

0 like 0 dislike
answered by
0 like 0 dislike
$comments = $news->comments()->with('user')->get();

or
$post = Post::with('comments', 'comments.user')->where('id', $id)->first();
answered by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
24,509 questions
45,932 answers
0 comments
346 users