Is it possible to have a lot of relationship to the same model?

Hi, is there any way to reference models to the same entity?

Like:

App/Models/User.js



exams() {
    return this.belongsToMany('App/Models/Exam','user_id','exam_id','id','id').pivotTable('user_make_exam').withTimestamps()
  }

  exams() {
    return this.hasMany('App/Models/Exam', 'id', 'exam_id')
  }
  }


App/Models/Exam.js

users() {
        return this.belongsToMany('App/Models/User','exam_id','user_id','id','id').pivotTable('user_make_exam').withTimestamps()
    }

    users() {
        return this.belongsTo('App/Models/User','id','exam_id')
    }

I have a relationship N:N and 1:N.

Is that possible ?

1 Like

Yes you can, but you can’t have two methods / relations with same name. Otherwise how would app know which users() method you have in mind currently?

Name one allUsers and other users or something that makes sense in your api.

1 Like