【发布时间】:2019-12-23 20:51:24
【问题描述】:
我目前有 API 返回的以下 json 响应。我可以使用 Laravel Eloquent 将其退回。有几个用户,每个用户都有几张收据。收据具有类型和状态。我想尝试获取与其类型和状态相关的每张收据的总金额。我能够使用
返回以下 json 响应$this->user->with('receipts')->has('receipts')->get(['id', 'name']);
我尝试过使用多种 laravel 集合方法https://laravel.com/docs/5.8/collections#available-methods 但我仍然无法得到想要的响应。
{
"id": 1,
"name": "kent",
"receipts": [
{
"id": 1,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 100
},
{
"id": 2,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 100
},
{
"id": 3,
"user_id": 1,
"type_id": 2,
"status": 1,
"amount": 50
},
{
"id": 4,
"user_id": 1,
"type_id": 2,
"status": 0,
"amount": 30
},
{
"id": 5,
"user_id": 1,
"type_id": 2,
"status": 0,
"amount": 30
},
{
"id": 6,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 20
},
{
"id": 7,
"user_id": 1,
"type_id": 1,
"status": 1,
"amount": 10
}
]
},
{
"id": 2,
"name": "allison",
"receipts": [
{
"id": 9,
"user_id": 2,
"type_id": 1,
"status": 0,
"amount": 20
}
]
}
]
我希望得到以下内容
{
"id": 1,
"name": "kent",
"receipts": [
{
"performance and deleted": 220,
"performance and not deleted": 10,
"project and deleted": 60,
"project and deleted": 50
}
]
},
{
"id": 2,
"name": "allison",
"receipts": [
{
"performance and deleted": 20,
"performance and not deleted": 0,
"project and deleted": 0,
"project and not deleted": 0
}
]
}
]
【问题讨论】:
标签: php laravel laravel-5 eloquent