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 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题