phparrayslaravel-5grouping

Group Laravel collection by the given key


In Laravel I want to change response if same user array have multiple response.

I get a response with this function:

controller.php

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

This is my response:

{
    "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."
}

This is fine my problem is I have 4 array with 2 user instead showing that way I want to show like this:

{
    "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."
}

How to achive this if same user comes under single array.


Solution

  • The ideal way to do this would be by using collection pipelines. Your original controller method would become the following;

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

    The groupBy method will split the collection results out into other collections, grouped by the provided column. Because the toArray() method cascades, you'll get a nice array back.