Relate table to itself.

Hello guys!

I have a comment table
id
comment
parent_id

For a comment may be the answer of another comment, so the id of the parent comment.

But I don’t know how to apply this to the model (hasmany … belongsto …)

Is there a way to do this or will I have to make a pivot table? to save the id of these relationships?

Hello,

You need relations:

parentComment(){
return this.hasMany(‘App/Models/Comment’, ‘id’, ‘parent_id’)
}

you can with all parent comments

await Comment
.query()
.with(‘parentComment’)
.fetch()