Skip to content

Instantly share code, notes, and snippets.

@vicainelli
Created November 25, 2019 19:57
Show Gist options
  • Save vicainelli/f00d2e079d9e5f0c7ee211d806f9ba7f to your computer and use it in GitHub Desktop.
Save vicainelli/f00d2e079d9e5f0c7ee211d806f9ba7f to your computer and use it in GitHub Desktop.
Payload com relacionamentos filtrados

O método está assim:

  async show ({ params }) {
    const id = Hashids.decode(params.id)
    const camping = await Camping.query()
      .where('id', '=', id)
      .with('city')
      .with('region')
      .with('country')
      .fetch()
    return camping
  }

Gostaria que fosse ser assim:

[
  {
    "id": 2,
    "user_id": 1,
    "country_id": 1,
    "region_id": 1,
    "city_id": 1,
    "name": "Camping do Mazinho",
    "address": null,
    "lat": 0,
    "lng": 0,
    "claimed": 0,
    "created_at": "2019-11-25 17:46:11",
    "updated_at": "2019-11-25 17:46:11",
    "hashids": "2YoWY0",
    "city": {
      "name": "Maresias"
    },
    "region": {
      "name": "São Paulo",
      "code": "sp"
    },
    "country": {
      "name": "Brasil",
      "code": "br"
    }
  }
]

Mas eu tenho isso:

[
  {
    "id": 2,
    "user_id": 1,
    "country_id": 1,
    "region_id": 1,
    "city_id": 1,
    "name": "Camping do Mazinho",
    "address": null,
    "lat": 0,
    "lng": 0,
    "claimed": 0,
    "created_at": "2019-11-25 17:46:11",
    "updated_at": "2019-11-25 17:46:11",
    "hashids": "2YoWY0",
    "city": {
      "id": 1,
      "country_id": 1,
      "region_id": 1,
      "name": "Maresias",
      "created_at": "2019-11-25 17:46:06",
      "updated_at": "2019-11-25 17:46:06",
      "hashids": "vY8qxN"
    },
    "region": {
      "id": 1,
      "country_id": 1,
      "name": "São Paulo",
      "code": null,
      "created_at": "2019-11-25 17:14:22",
      "updated_at": "2019-11-25 17:14:22",
      "hashids": "vY8qxN"
    },
    "country": {
      "id": 1,
      "name": "Brasil",
      "code": "br",
      "created_at": "2019-11-25 17:12:47",
      "updated_at": "2019-11-25 17:12:47"
    }
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment