0

我的代码..

阿多尼斯V4

使用下面的代码,我可以准确地生成我想要的输出,但我认为我做错了。我在任何论坛上都没有找到任何相关的东西,我相信我不知道我在找什么

  var results = await ReboxSurplus.query()
  .where(function () {
    if (state) this.where("state", state);
    if (city) this.where("city", city);
  })
  .with('port')
  .fetch();

  for (const row of results.rows) {
    row['prices'] = await ReboxPrice.query().where('state', row.state).where('city', row.city).where('port_car_id', row.port_car_id).fetch()
  }

我的输出..

我想获得的json正是这个,我相信解决方案是直接在模型中忘记这个“增强”,有人可以帮助我吗?

 {
"id": 29305,
"port_car_id": 1,
"state": "RJ",
"city": "Rio de Janeiro",
"rebox_commission": 0,
"created_at": "2020-12-22 18:43:03",
"updated_at": "2020-12-22 18:43:03",
"prices": [
  {
    "id": 890641,
    "price": 125,
    "km": 10,
    "per_km": 1.5,
    "commission": 40,
    "created_at": "2020-12-21 13:59:59",
    "updated_at": "2020-12-21 13:59:59",
    "port_car_id": 1,
    "state": "RJ",
    "city": "Rio de Janeiro"
  },
  {
    "id": 890642,
    "price": 140,
    "km": 20,
    "per_km": 1.5,
    "commission": 40,
    "created_at": "2020-12-21 13:59:59",
    "updated_at": "2020-12-21 13:59:59",
    "port_car_id": 1,
    "state": "RJ",
    "city": "Rio de Janeiro"
  },
  {
    "id": 890643,
    "price": 160,
    "km": 40,
    "per_km": 1.5,
    "commission": 40,
    "created_at": "2020-12-21 13:59:59",
    "updated_at": "2020-12-21 13:59:59",
    "port_car_id": 1,
    "state": "RJ",
    "city": "Rio de Janeiro"
  }
],
"port": {
  "id": 1,
  "port": "Carro de Passeio",
  "percent_value": 40,
  "percent_extra": 0,
  "created_at": "2020-11-12 16:02:46",
  "updated_at": "2020-11-12 16:02:46",
  "state": null,
  "city": null
}

},

4

0 回答 0