【问题标题】:Laravel/Lumen: Handling the fetchresults of Model::With('Relation')Laravel/Lumen:处理 Model::With('Relation') 的 fetchresults
【发布时间】:2020-05-19 07:55:15
【问题描述】:

我有三个相关的表: 由 Table extensiontable_itc 和 extensiontable_sysops 引用的 Table Coretable。

我现在想从 coretable 中获取 extensiontable_itc 和 extensiontable_sysops 引用的所有记录,为此,我运行以下代码:

$join = coretable::with('extensiontable_itc','extensiontable_sysops')->get();

这种工作,我得到以下结果:

[{
  "id": 1,
  "Internal_key": "TESTKEY_1",
  "created_at": null,
  "updated_at": null,
  "extensiontable_itc": {
    "id": 1,
    "coretable_id": 1,
    "description": "EXTENSION_iTC_1",
    "created_at": null,
    "updated_at": null
  },
  "extensiontable_sysops": {
    "ID": 1,
    "coretable_id": 1,
    "description": "EXTENSION_SYSOPS_1"
  }
}, {
  "id": 2,
  "Internal_key": "TESTKEY_2",
  "created_at": null,
  "updated_at": null,
  "extensiontable_itc": {
    "id": 4,
    "coretable_id": 2,
    "description": "EXTENSION_ITC_2",
    "created_at": null,
    "updated_at": null
  },
  "extensiontable_sysops": {
    "ID": 2,
    "coretable_id": 2,
    "description": "EXTENSION_SYSOPS_2"
  }
}, {
  "id": 3,
  "Internal_key": "TESTKEY_3",
  "created_at": null,
  "updated_at": null,
  "extensiontable_itc": {
    "id": 5,
    "coretable_id": 3,
    "description": "EXTENSION_ITC_3",
    "created_at": null,
    "updated_at": null
  },
  "extensiontable_sysops": {
    "ID": 3,
    "coretable_id": 3,
    "description": "EXTENSION_SYSOPS_3"
  }
}, {
  "id": 4,
  "Internal_key": "TESTKEY_4",
  "created_at": null,
  "updated_at": null,
  "extensiontable_itc": {
    "id": 6,
    "coretable_id": 4,
    "description": "EXTENSION_ITC_4",
    "created_at": null,
    "updated_at": null
  },
  "extensiontable_sysops": {
    "ID": 4,
    "coretable_id": 4,
    "description": "EXTENSION_SYSOPS_4"
  }
}, {
  "id": 5,
  "Internal_key": "TESTKEY_5",
  "created_at": null,
  "updated_at": null,
  "extensiontable_itc": {
    "id": 7,
    "coretable_id": 5,
    "description": "EXTENSION_ITC_5",
    "created_at": null,
    "updated_at": null
  },
  "extensiontable_sysops": {
    "ID": 5,
    "coretable_id": 5,
    "description": "EXTENSION_SYSOPS_5"
  }
}]

如您所见,引用 coretable 上相应记录的记录被放入一个数组中,该数组本身成为包含 coretable 数据的整个数组的一个元素。

现在,我想获取所有这些数据,除了:

coretable 中的 ids

created_at/updated_at

外键/coretable_ids

我已经使用了集合方法“pluck()”,但它似乎不适合访问这个索引中包含所有这些重复项的多级数组的目的。

有什么方便的方法吗?还是我必须使用循环手动完成?

【问题讨论】:

标签: php laravel eloquent orm


【解决方案1】:

https://laravel.com/docs/5.8/eloquent-serialization#hiding-attributes-from-json

有两种方法可以隐藏字段。第一个是将需要的字段放入模型类内部的$hidden数组中,所有这些字段都不会包含在模型实例中。第二种是使用makehidden函数。这允许您动态隐藏字段,例如取决于某些条件。

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2020-11-28
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多