我有两个表posts
和post_metas
,其模式如下:
POSTS
+------------+------------------+
| Field | Type |
+------------+------------------+
| id | int(10) unsigned |
| title | varchar(255) |
| content | longtext |
POST_METAS
+------------+------------------+
| Field | Type |
+------------+------------------+
| id | int(10) unsigned |
| post_id | int(10) unsigned |
| key | varchar(255) |
| value | longtext |
使用简单的一对多关系,我得到了以下输出。
[
{
"id": 1,
"title": "Dibbert LLC",
"content": null,
"post_metas": [
{
"key": "code",
"value": "YSRSLBZ7"
},
{
"key": "agent_id",
"value": "32"
}
]
}
]
我想知道是否可能将文章元数据作为对象而不是数组,如下所示:
[
{
"id": 1,
"title": "Dibbert LLC",
"content": null,
"post_metas": {
"code": "YSRSLBZ7",
"agent_id": "32"
}
}
]
post
并获取post_meta
的呢?Laravel 确实会返回一个对象集合,看起来就像你想要的输出。 - Kenny Horna