【发布时间】: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()”,但它似乎不适合访问这个索引中包含所有这些重复项的多级数组的目的。
有什么方便的方法吗?还是我必须使用循环手动完成?
【问题讨论】:
-
我的想法完全一样,@V-K 你应该把它放在答案中