Learning
Learning

Reputation: 79

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.

Upvotes: 2

Views: 5096

Answers (2)

SRK
SRK

Reputation: 3496

You need to loop through the result and modify it as per your requirement.I have tested the below code its working fine. Try using it.

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

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

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

Thanks.

Upvotes: -3

ollieread
ollieread

Reputation: 6143

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.

Upvotes: 6

Related Questions