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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)