dimanche 18 août 2019

How to calculate the total amount for similar keys in PHP using Laravel eloquent/collections

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 the below

    {
        "id": 1,
        "name": "kent",
        "receipts": [
            {
                "type_id1 and status0": 220,
                "type_id1 and status1": 10,
                "type_id2 and status0": 60,
                "type_id2 and status1": 50
            }
        ]
    },
    {
        "id": 2,
        "name": "allison",
        "receipts": [
            {
                "type_id1 and status0": 20,
                "type_id1 and status1": 0,
                "type_id2 and status0": 0,
                "type_id2 and status1": 0
            }
        ]
    }
]



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/31JLCA9
via IFTTT

Aucun commentaire:

Enregistrer un commentaire