Closed pllealfunes closed 2 years ago
Your "$commentAuthor" is a single variable, that you keep overwriting in your controller, in this line $commentAuthor = User::where('id','=', $comment->user_id)->first();
"Jamal" being the last user in the comments table, that is what the single variable will hold before being passed to the view.
Not sure about your model, but the quickest fix is to have a display only data structure that you pass to your view. Like
$commentRows = [];
foreach(Comment::where('post_id', '=', $id)->get() AS $comment) {
$usr = User::where('id','=', $comment->user_id)->first();
$commentRows[] = [ "commentText" => $comment['comment'], "commentAuthor" => $usr['name'] ];
]
...
return view(....., [ ... 'comments' => $commentRows, .....]);
and pass $rows
to the view with all information filled in.
If you have a foreign key from your comments
table to the users
table (which it looks like you do), you could try $comment->user['name']
, instead of querying the users
table for every comment in my above example.
@gkorodi Thanks for the reply.
I ran to a similar issue when it came to displaying the author of every blog post but managed another way. I saw online something similar to the $comment->user['name']
you mentioned except I think the format was more like $comment->user->name
. I tried both and I'm not sure why either don't work. I would like to try it because as you said it does mean less querying the users table. Below is my Comment Model. Since my public functions is calledusers
I did try $comment->users['name']
and $comment->users->name
.
Comment Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use HasFactory;
public function users()
{
return $this->belongsTo(User::class, 'comments');
}
}
Recommend removing the 'comments'
part. Check out https://laravel.com/docs/9.x/eloquent-relationships#one-to-one-defining-the-inverse-of-the-relationship to see that the user_id
on the comments
table could already serve a a foreign key. Depending on how you created the table (via migration or not).
If the foreign key is indeed established from the comments
table, via the belongsTo
function, then you could follow example.
Also, call the function user
instead of users
. It is part of the convention for eloquent.
@gkorodi Thank you it worked! I also changed my Post Model to be the same as the Comment Model and now I am able to use $comment->user->name
and $post->user->name` in my views.
Table relationships are still a little confusing to me 😅 but thanks for the help!
@gkoroki Thanks! This helped me too.
Hey guys,
For the comments section of my blog I would like to display the name of the person who wrote a comment next to their comment but I'm running into the issue where I can't display the right name to each comment.
For example Post 1 has 3 comments. The first comment is from Jill and the other two are from Jamal but all of my comments are showing as if Jamal wrote them. In myphpadmin I can see that the first comment belongs to Jill. How can I fix it so that each comment shows the correct name of the user who wrote it?
show.blade
PostController