douzong6649 2019-08-18 09:03
浏览 53
已采纳

如何使用Laravel eloquent / collections计算PHP中类似键的总量

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": [
            {
                "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
            }
        ]
    }
]
  • 写回答

3条回答 默认 最新

  • 普通网友 2019-08-18 10:09
    关注

    You should be able to get the sum of amount with

    $this->user->with(['receipts' => function($query) {
        $query->selectRaw("SUM(amount) as amount, type_id, status, user_id")->groupBy('type_id', 'status', 'user_id');
    }])->has('receipts')->get(['id', 'name']);
    

    You can use collection methods to get the desired output

    $this->user->with(['receipts' => function($query) {
        $query->selectRaw("SUM(amount) as amount, type_id, status, user_id")->groupBy('type_id', 'status', 'user_id');
    }])->has('receipts')->get(['id', 'name'])
    ->each(function ($user) {
        $user->setRelation(
            'receipts',
            $user->receipts->mapWithKeys(function ($receipt) {
                return [
                    $receipt->type_id . ' and ' . $receipt->status => $receipt->amount // format the key as you wish
                ];
            })
        );
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LSPI算法的大问题
  • ¥15 java导出word 动态填充数据
  • ¥15 python SSH连接设备使用STD读配置卡死的问题
  • ¥20 扑克的算牌公式及软件制作
  • ¥20 如何通过云图中RPG去计算云图上不同位置的值?
  • ¥15 请问不小心下载到了钓鱼软件怎么办?
  • ¥15 求国博抢票 求国博抢票 有的私
  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)