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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格