Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...