Laravel 加入 JSON 的关系
Laravel Join Relations for JSON
我在 Laravel 中使用 Eloquent 关系将我的数据输出到 JSON。当我使用 Eloquent 关系时,它们作为一个对象出现在 JSON 中,但我只需要 JSON 输出,其中没有对象或 json,如下所示:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
},
而不是这个(LARVEL 中一对多到 JSON 的关系的结果):
$activity = Activity::find(1)->load('history')->toJSON();
结果:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history": [
{
"id": 1,
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
}
]
},
知道如何做到这一点吗?我可以使用 DB join() 来完成,但是有没有办法使用 Eloquent 来完成?此外 :
$activity = \App\Models\Activity::find(1)
->join('activity_history', 'activity_history.id', '=',
'activities.history_id')->select('*')->get()
->toJSON();
因为这不是最好的方法,因为我正在使用列名和 table 名称,我必须去后面的数据库中查看它们
您可以在获得该结果时通过展平数组来解决此问题。假设您 $object->history
要获得具有历史关系的结果,只需执行以下操作:
Arr::dot($object->history);
所以你会得到一个像这样的数组:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history.id": 1,
"history.type_id": 2,
"history.comment": "Horoshiy Specialist",
"history.result_id": 2,
"history.result_comment": "Prowerili anketu i obrazowanie",
"history.start_time": null,
"history.end_time": null,
}
我在 Laravel 中使用 Eloquent 关系将我的数据输出到 JSON。当我使用 Eloquent 关系时,它们作为一个对象出现在 JSON 中,但我只需要 JSON 输出,其中没有对象或 json,如下所示:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
},
而不是这个(LARVEL 中一对多到 JSON 的关系的结果):
$activity = Activity::find(1)->load('history')->toJSON();
结果:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history": [
{
"id": 1,
"type_id": 2,
"comment": "Horoshiy Specialist",
"result_id": 2,
"result_comment": "Prowerili anketu i obrazowanie",
"start_time": null,
"end_time": null,
}
]
},
知道如何做到这一点吗?我可以使用 DB join() 来完成,但是有没有办法使用 Eloquent 来完成?此外 :
$activity = \App\Models\Activity::find(1)
->join('activity_history', 'activity_history.id', '=',
'activities.history_id')->select('*')->get()
->toJSON();
因为这不是最好的方法,因为我正在使用列名和 table 名称,我必须去后面的数据库中查看它们
您可以在获得该结果时通过展平数组来解决此问题。假设您 $object->history
要获得具有历史关系的结果,只需执行以下操作:
Arr::dot($object->history);
所以你会得到一个像这样的数组:
{
"id": 1,
"name": "Status changed",
"history_id": null,
"admin_id": 4,
"created_at": "2021-05-21T16:10:08.000000Z",
"updated_at": "2021-04-21T16:10:08.000000Z",
"history.id": 1,
"history.type_id": 2,
"history.comment": "Horoshiy Specialist",
"history.result_id": 2,
"history.result_comment": "Prowerili anketu i obrazowanie",
"history.start_time": null,
"history.end_time": null,
}