按给定键分组Laravel集合

时间:2018-03-12 11:00:26

标签: arrays api laravel-5

Laravel中的

如果相同的用户数组有多个响应,我想更改响应

我得到了这个函数的回复

Controller.php这样

public function index(){
    $reports = Report::all();
    return $this->sendResponse($reports->toArray(), 'User Reports successfully.');
}

这是我的回复

{
    "success": true,
    "data": [
        {
            "id": 66,
            "cuid": 20,
            "name": "my1",
            "created_at": "2018-03-09 00:00:00",
            "updated_at": "2018-03-09 00:00:00",
            "time": "07:19 PM",
            "status": "D"
        },
        {
            "id": 65,
            "cuid": 20,
            "name": "my1",
            "created_at": "2018-03-07 00:00:00",
            "updated_at": "2018-03-07 00:00:00",
            "time": "07:39 PM",
            "status": "D"
        },
        {
            "id": 64,
            "cuid": 21,
            "name": "my2",
            "created_at": "2018-03-02 00:00:00",
            "updated_at": "2018-03-05 00:00:00",
            "time": "07:01 PM",
            "status": "D"
        },
        {
            "id": 63,
            "cuid": 20,
            "name": "my2",
            "created_at": "2018-03-02 00:00:00",
            "updated_at": "2018-03-02 00:00:00",
            "time": "06:44 PM",
            "status": "D"
        }     
    ],
    "message": "User Reportsssss successfully."
}

这很好我的问题是我有4个阵列,有2个用户insted显示我希望显示这样的方式

{
    "success": true,
    "data": [
        my1:{
                {
                    "id": 66,
                    "cuid": 20,
                    "name": "my1",
                    "created_at": "2018-03-09 00:00:00",
                    "updated_at": "2018-03-09 00:00:00",
                    "time": "07:19 PM",
                    "status": "D"
                }
                {
                    "id": 65,
                    "cuid": 20,
                    "name": "my1",
                    "created_at": "2018-03-07 00:00:00",
                    "updated_at": "2018-03-07 00:00:00",
                    "time": "07:39 PM",
                    "status": "D"
                }
        },
         my2:{
                {
                    "id": 63,
                    "cuid": 21,
                    "name": "my2",
                    "created_at": "2018-03-07 00:00:00",
                    "updated_at": "2018-03-07 00:00:00",
                    "time": "07:39 PM",
                    "status": "D"
                }
                {
                    "id": 64,
                    "cuid": 21,
                    "name": "my2",
                    "created_at": "2018-03-02 00:00:00",
                    "updated_at": "2018-03-05 00:00:00",
                    "time": "07:01 PM",
                    "status": "D"
                }
        }
    ],
    "message": "User Reportsssss successfully."
}

如果同一个用户属于单个数组

,如何实现此目的

2 个答案:

答案 0 :(得分:2)

执行此操作的理想方法是使用收集管道。您的原始控制器方法将成为以下内容;

public function index(){
    $reports = Report::all();
    return $this->sendResponse($reports->groupBy('name')->toArray(), 'User Reports successfully.');
}

groupBy方法会将收集结果拆分为其他集合,按提供的列分组。由于toArray()方法级联,您将获得一个不错的数组。

答案 1 :(得分:-2)

你需要遍历结果并根据你的要求修改它。我已经测试了下面的代码,它的工作正常。尝试使用它。

$reports = $reports->toArray();
$finalArray = [];

foreach($reports as $key=>$value) {
    $name = $value['name'];
    $finalArray[$name][] = $value;
}

return $this->sendResponse($finalArray, 'User Reports successfully.');

感谢。