Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

group by - Laravel : show the field name which grouped by

I want to create an API ,I have this controller which return data grouped by date:

public function dated_matchs()
    {
        $mdate = Match::all()->groupBy('m_date');
        return $mdate;
    }

the result is like this :

{
  "2021-02-26": [
    {
      "id": 2,
      "week_season": 1,
      "week_number": 1,
      "m_hour": "21:31:28",
      "m_date": "2021-02-26",
    }
  ],
  "2021-02-27": [
    {
      "id": 7,
      "week_season": 1,
      "week_number": 1,
      "m_hour": "21:37:04",
      "m_date": "2021-02-27",
    }
  ]
}

I want to be something like this:

{
  "date":"2021-02-26"
  "matches": [
    {
      "id": 2,
      "week_season": 1,
      "week_number": 1,
      "m_hour": "21:31:28",
      "m_date": "2021-02-26",
    },
  ],
  "date":"2021-02-27"
  "matches": [
    {
      "id": 7,
      "week_season": 1,
      "week_number": 1,
      "m_hour": "21:37:04",
      "m_date": "2021-02-27",
    }
  ]
}

How can I add date name to the data returned from API

question from:https://stackoverflow.com/questions/65943986/laravel-show-the-field-name-which-grouped-by

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use ->map()->values() over your grouped data like

$collection = collect([
  "2021-02-26"=>[
      "id"=>2,
      "week_season"=> 1,
      "week_number"=>1,
      "m_hour"=> "21:31:28",
      "m_date"=> "2021-02-26",
    
  ],
  "2021-02-27"=> [
      "id"=> 7,
      "week_season"=> 1,
      "week_number"=>1,
      "m_hour"=> "21:37:04",
      "m_date"=>"2021-02-27",
    
  ]
]);

$results = $collection->map(function ($item, $key) {
    return ["date"=> $key, "matches"=>$item];
})->values();

echo "<pre>";
print_r($results->toArray());
echo "</pre>";

which will produce output as

Array
(
    [0] => Array
        (
            [date] => 2021-02-26
            [matches] => Array
                (
                    [id] => 2
                    [week_season] => 1
                    [week_number] => 1
                    [m_hour] => 21:31:28
                    [m_date] => 2021-02-26
                )
        )

    [1] => Array
        (
            [date] => 2021-02-27
            [matches] => Array
                (
                    [id] => 7
                    [week_season] => 1
                    [week_number] => 1
                    [m_hour] => 21:37:04
                    [m_date] => 2021-02-27
                )
        )
)

Finally it will look something like

$mdate = Match::all()
               ->groupBy('m_date');
               ->map(function ($item, $key) {
                   return ["date"=> $key, "matches"=>$item];
               })->values();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...