Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lonare's avatar

Discussion Comment User relationship

Hi i have three tables

Users
- id
- name
- email
Discussion
- id
- title
Comment
- id
- comment

and i have a pivot table

comment_discussion
- id
- discussion_id
- comment_id
- user_id

now i am fetching all related comments from the discussion like this:

$comments      = $discussion->comments;

my question is as my pivot table has a connection to user_id also how will i get user details ?

i have added this to user model:

public function comments()
    {
        return $this->belongsToMany('Comment', 'comment_discussion')->withPivot('user_id');;
    }

Any help will be appreciated.

0 likes
15 replies
Lonare's avatar

Hi Alfrednutile,

even if i use hasManyThrough relationship how will i fetch userdetails ?

how to change profile picture here :) i tried going to my profile but no option to upload avatar....!!!

Cheers...!!! HLonare

bestmomo's avatar

What you need is many through many, look at this post

For avatar it's just linked to profil email and gravatar.

seb7's avatar

Just remove user_id from pivot table. User is the comment writer, so user_id shouldBe a property of Comment.

Lonare's avatar

Hi seb7,

But then how will i fetch user details ?

this is how my comment model looks like:

class Comment extends Eloquent {

    protected $table = 'comments';
    protected $primaryKey = 'id';
    protected $fillable = ['comment'];


    public function user()
    {
        return $this->belongsTo('User');
    }

    public function discussion()
    {
        return $this->belongsTo('Discussion');
    }

}

bestmomo's avatar

Looks like your comments table must be the pivot between users and discussions.

seb7's avatar

Simply '''$user = $comment->user;'''

pmall's avatar

Discutions hasMany Comments

Comments belongsTo User

Why using a pivot table ? There is no many to many relation.

bestmomo's avatar

@pmall seems that there is a many to many relation that results of 2 has many relations, so the comments table becomes the pivot in this relation.

Lonare's avatar

hi @bestmomo

See my tables i have only one pivot table i.e., comment_discussion which has 3 fields - comment_id - discussion_id - user_id

and i am fetching all comments for a discussion like this

$comments      = $discussion->comments;

but i am not receiving any user data...

pmall's avatar

Try describing your application because this schema is strange.

Lonare's avatar

ok I am building a project management system in which we can create a discussion.

Inside the discussion page -> you can add your comments

and obviously each comment belongs to one user

so i just want to fetch all comments on particular discussion ( which i have already done successfully ) but i am not receiving any user information like username / useravatar etc.

i have tried manytomany, morphMany, hasManyThrough relationships

Help ??

pmall's avatar
pmall
Best Answer
Level 56

So your schema is over-complicated.

You just need three tables, Discussions, Comments, Users.

  • Discussions hasMany Comments / Comments belongsTo a Discussion (discussion_id on comments table)
  • User hasMany Comments / Comments belongsTo a User (user_id on comments table)

Then :

$discussion = Discussion::findOrFail($discussion_id);

echo $discussion->title;

foreach($discussion->comments as $comment)
{
  echo $comment->comment;
  echo $comment->user->name;
}

1 like
Lonare's avatar

hi @pmall

thats what i dont want to do. I dont want to add

discussion_id
user_id

in comment table

I have created a pivot table where it connects discussion with all comments

comment_discussion
- id
- discussion_id
- comment_id
- user_id

Do you think there is a way i can fetch user detail by using this pivot table ?

pmall's avatar

Nothing is wrong with having user_id and discussion_id in comments table. Why don't you want to do that ?

It is the conventions that make eloquent easy to use. It will be hell with a triple-pivot table.

Please or to participate in this conversation.