I currently have the below json response that the API is returning. I am able to return it using Laravel Eloquent. There are several users and each user has a several receipts. A receipt has types and status. I want to try to get the total sum amount for each receipt that is related to its type and status. I was able to return the below json response using
$this->user->with('receipts')->has('receipts')->get(['id', 'name']);
I have tried using multiple laravel collections methods https://laravel.com/docs/5.8/collections#available-methods But I am still unable to get the desired response.
{ "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 } ] } ]
I expect to get this:
{ "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 } ] }
My main concern is to use laravel collection methods and easy to read code to get my expected result
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NxGHi8
via IFTTT
Aucun commentaire:
Enregistrer un commentaire