Return of Pivot data

Hi all,

There’s any way to return my pivot data without “pivot” property in json ?

I need my data like this:

{
  "id": 1,
  "name": "Rafael",
  "gender": null,
  "roles": [
    {
      "id": 1,
      "name": "Manager",
      "start_period": "01/2018",
      "end_period": "12/2019",
      "status": null
    }
  ]
}

But my result is:

{
  "id": 1,
  "name": "Rafael",
  "gender": null,
  "roles": [
    {
      "id": 1,
      "name": "Manager",
      "pivot": {
        "role_id": 1,
        "person_id": 1,
        "start_period": "01/2018",
        "end_period": "12/2019",
        "status": null
      }
    }
  ]
}

Person Model:

class Person extends Model {
  static get table () {
    return 'persons'
  }
  roles () {
    return this
      .belongsToMany('App/Models/Role')
      .withPivot(['start_period', 'end_period', 'status']);
  }
}

Controller method:

  async show ({ params }) {
    const person = await Person.findOrFail(params.id);
    await person.load('roles');
    return person;
  }

Thanks.

Rafael

1 Like

Unfortunately, I do not think you can do anything about how the pivot query result is returned. However, you can transform your result to what you want:

async show ({ params }) {
    const person = await Person.findOrFail(params.id);
    await person.load('roles');
    person.roles = person.roles.map((role) => {
      return {
        id: role.id,
        name: role.name,
        start_period: role.pivot.start_period,
        end_period: role.pivot.end_period,
        status: role.pivot.status
      }
    })
    return person;
  }

Cheers!

2 Likes

Hi man,
Thanks for the answer.

Yeah … I did something like that.
But I would like a more “under the hood” option.

Thanks !

1 Like

Hi man,
Unfortunately this solution does not work :frowning:

async show ({ params }) {
    const person = await Person.findOrFail(params.id);
    await person.load('roles');
    console.log(person);
    person.roles = person.roles.map((role) => {
      return {
        id: role.id,
        name: role.name,
        start_period: role.pivot.start_period,
        end_period: role.pivot.end_period,
        status: role.pivot.status
      }
    })
    return person;
  }

TypeError
person.roles.map is not a function


console.log(person)

Person {
  __setters__: [
    '$attributes',
    '$persisted',
    'primaryKeyValue',
    '$originalAttributes',
    '$relations',
    '$sideLoaded',
    '$parent',
    '$frozen',
    '$visible',
    '$hidden'
  ],
  '$attributes': {
    id: 1,
    name: 'Rafael',
    birthday: 1986-04-02T03:00:00.000Z,
    rg: '',
    cpf: '',
    occupation: '',
    gender: 'M',
    maritalStatus: 'C',
    nationality: '',
    placeBirth: '',
    ufPlaceBirth: '',
    fatherName: '',
    motherName: '',
    zip: '',
    address: '',
    city: '',
    neighborhood: '',
    state: '',
    workPlace: '',
    email: '',
    cellPhone: '99999999',
    phone: '22222',
    workPhone: '33333',
    created_at: null,
    updated_at: 2020-06-17T02:10:51.000Z
  },
  '$persisted': true,
  '$originalAttributes': {
    id: 1,
    name: 'Rafael',
    birthday: 1986-04-02T03:00:00.000Z,
    rg: '',
    cpf: '',
    occupation: '',
    gender: 'M',
    maritalStatus: 'C',
    nationality: '',
    placeBirth: '',
    ufPlaceBirth: '',
    fatherName: '',
    motherName: '',
    zip: '',
    address: '',
    city: '',
    neighborhood: '',
    state: '',
    workPlace: '',
    email: '',
    cellPhone: '99999999',
    phone: '22222',
    workPhone: '33333',
    created_at: null,
    updated_at: 2020-06-17T02:10:51.000Z
  },
  '$relations': {
    roles: VanillaSerializer { rows: [Array], pages: null, isOne: false }
  },
  '$sideLoaded': {},
  '$parent': null,
  '$frozen': false,
  '$visible': undefined,
  '$hidden': undefined
}
1 Like

Try to convert person to JSON before mapping.