Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
How to build the relationship between the three tables in laravel? - bug-code.com Q&A

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

0 like 0 dislike
46 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 | 46 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
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers

0 comments

12.3k users

24,788 questions
46,220 answers
0 comments
12,337 users