In my controller I have:
public function index()
{
$comments = Comment::paginate(10);
return view('comments.index', compact('comments'));
}
And in my view I use {{ $comment->id }}, {{ $comment->message }}`and so forth to get the data from the comments table.
Now I want to display the user in that comments view that posted the comment.
I have another table named comments_user that stores the user_id and comment_id so in the comments view I basically need to look up the comments ID in the comments_user table, get the user_id and lookup the name in the users table so I can display it in the comments view.
My example structure to make things easier and what I expect:
comments
+----+---------------------+--+
| id | message | |
+----+---------------------+--+
| 1 | this is a test post | |
| 2 | commenting test | |
| 3 | hello world! | |
+----+---------------------+--+
users
+----+------+
| id | name |
+----+------+
| 1 | dan |
| 2 | bob |
| 3 | mike |
+----+------+
comments_user
+---------+------------+
| user_id | comment_id |
+---------+------------+
| 3 | 1 |
| 2 | 3 |
| 3 | 2 |
+---------+------------+
Expected comments view:
+------+---------------------+
| mike | this is a test post |
| bob | commenting test |
| dan | hello world! |
+------+---------------------+
How could I call something like {{ $comment->comments_user }} to have the name appearing next to the comment (in the comments view) based on the above structure?
Have you searched the forum for looping and one too many and hasmany there were many answers on this particular subject already. Also search Laracast. Also see this post. ===== http://laravel.io/forum/04-22-2015-large-collection-many-levels-getting-views-out and ==== http://laravel.io/forum/04-25-2015-beginner-question-trying-to-display-articles-containing-the-same-category
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community