1

I do have an Eloquent query which kinda looks like this:

 DB::table('football')->orderBy('team_name', 'asc')->groupBy('team_id', 'team_name', 'info')->get(['team_id', 'team_name', 'info']);

This statement returns me only distinct values of the combination of those 3 columns.

The returned Eloquent collection object has the following structure:

[
  {"team_id": "2000", "team_name": "FC Barcelona", "info": "03"},
  {"team_id": "2000", "team_name": "FC Barcelona", "info": "09"},
  {"team_id": "2000", "team_name": "FC Barcelona", "info": "D3"}
  {"team_id": "3000", "team_name": "FC Chelsea", "info": "03"},
  {"team_id": "3000", "team_name": "FC Chelsea", "info": "12"},
  {"team_id": "4000", "team_name": "Real Madrid", "info": "06"}
]

is there a way in Eloquent or PHP to convert the above structure into the following:

[
  {"team_id": "2000", "team_name": "FC Barcelona", "info": { "03", "09", "D3" } },
  {"team_id": "3000", "team_name": "FC Chelsea", "info": { "03", "12" } },
  {"team_id": "4000", "team_name": "Real Madrid", "info": { "06" } }
]

Thank you for your time and all the answers.

olympusgr
  • 57
  • 6
  • 1
    Does this answer your question? [How to use groupBy() in Eloquent ORM to group a collection](https://stackoverflow.com/questions/30361813/how-to-use-groupby-in-eloquent-orm-to-group-a-collection) – Itay Sep 03 '20 at 05:20
  • Maybe, I tried some things mentioned there but I never got the structure mentioned above :/ – olympusgr Sep 03 '20 at 06:59
  • Your may use group_concat on column info like this https://stackoverflow.com/questions/25847738/group-concat-laravel-eloquent – Anton Mikhaylenko Sep 03 '20 at 11:55

1 Answers1

1

In plain php you can transform your data as

$results = [
  ["team_id"=> "2000", "team_name"=> "FC Barcelona", "info"=> "03"],
  ["team_id"=> "2000", "team_name"=> "FC Barcelona", "info"=> "09"],
  ["team_id"=> "2000", "team_name"=> "FC Barcelona", "info"=> "D3"],
  ["team_id"=> "3000", "team_name"=> "FC Chelsea", "info"=> "03"],
  ["team_id"=> "3000", "team_name"=> "FC Chelsea", "info"=> "12"],
  ["team_id"=> "4000", "team_name"=> "Real Madrid", "info"=> "06"]
];


$data = [];
foreach($results as  $result){
    $key = $result["team_id"].$result["team_name"];
    if(isset($data[$key])){
        $data[$key]["info"][] = $result["info"];
    }else{
        $data[$key] = ["team_id"=> $result["team_id"], "team_name"=> $result["team_name"], "info"=> [$result["info"]]];
    }
}
$data = array_values($data);
echo '<pre>'. print_r($data, true).'</pre>';

Resultant array would look like

Array
(
    [0] => Array
        (
            [team_id] => 2000
            [team_name] => FC Barcelona
            [info] => Array
                (
                    [0] => 03
                    [1] => 09
                    [2] => D3
                )

        )

    [1] => Array
        (
            [team_id] => 3000
            [team_name] => FC Chelsea
            [info] => Array
                (
                    [0] => 03
                    [1] => 12
                )

        )

    [2] => Array
        (
            [team_id] => 4000
            [team_name] => Real Madrid
            [info] => Array
                (
                    [0] => 06
                )

        )

)

In laravel I guess you can smartly use mapToGroups() over collection

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118