Sal*_*lam 4 php laravel eloquent
我有两个表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 |
Run Code Online (Sandbox Code Playgroud)
使用简单的一对多关系,我得到这个输出
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 |
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以将 post metas 作为对象而不是数组,而不是像这样:
[
{
"id": 1,
"title": "Dibbert LLC",
"content": null,
"post_metas": [
{
"key": "code",
"value": "YSRSLBZ7"
},
{
"key": "agent_id",
"value": "32"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
您无法更改 Eloquent 返回关系集合的方式,也不应该更改,但您当然可以在响应中操作集合:
$model->post_metas->pluck('value', 'key');
Run Code Online (Sandbox Code Playgroud)
阅读所有可用的收集方法。
| 归档时间: |
|
| 查看次数: |
7089 次 |
| 最近记录: |