Complex belongsToMany Query

I have a User model and a Room model with 2 relationships. User has a hasMany relationship with Room:

// Rooms that the User owns
my_rooms () {
    return this.hasMany('App/Models/Room')
  }

And also has a many-to-many relationship with Room:

// Rooms that User participates in
rooms () {
    return this
      .belongsToMany('App/Models/Room', 'user_id', 'room_id')
      .pivotTable('user_room')
      .withTimestamps()
  }

I need to make a query that returns me all the rooms I own, all the rooms I participate in, and an instance of the Users who own these rooms. By executing the following code:

const user = auth.user
await user.loadMany(['my_rooms', 'rooms'])

console.log(user.toJSON())

Within the user object I have access to the rooms I own and the rooms I participate in, as well as the id of the owner of the room I participate in. However, I need an instance of the owner of each room I participate in for information such as name, email and so on.
Here is the impression of user.toJSON () :

{ id: 7,
  name: 'Italo',
  username: 'ita',
  email: 'italo.oliveira300@hotmail.com',
  password:
   '$2a$10$cBnG7.SeuSKZaj5EcDJafu14QQkEHIEoFqbhcGnpGh3P8351BgU2q',
  dateBirth: 2018-06-19T05:00:00.000Z,
  photo:
   '1573956977574-a.jpg',
  token: '238ece',
  is_active: 1,
  created_at: '2019-11-16 21:16:17',
  updated_at: '2019-11-16 21:16:36',
  my_rooms:
   [ { id: 1,
       user_id: 7,
       name: 'teste',
       subject: 'teste',
       description: 'asdasdas',
       photo: null,
       is_public: 1,
       token: 'bdc0d7',
       created_at: '2019-11-17 17:18:13',
       updated_at: '2019-11-17 17:18:13' },
     { id: 2,
       user_id: 7,
       name: 'Teste2',
       subject: 'teste2',
       description: 'asifjoasiofjaod',
       photo: null,
       is_public: 0,
       token: '0a6b16',
       created_at: '2019-11-17 18:30:53',
       updated_at: '2019-11-17 18:30:53' } ],
  rooms:
   [ { id: 3,
       user_id: 9,
       name: 'Test5',
       subject: 'test5',
       description: 'usdausdhuas',
       photo: null,
       is_public: 1,
       token: '5b4534',
       created_at: '2019-11-17 19:25:52',
       updated_at: '2019-11-17 19:25:52',
       pivot: [Object] } ] }
1 Like