Laravel add to new collection from loop

  laravel, laravel-collection

I have the following piece of code :

    $callReport = array();
    $callResult->each(function ($item) use (&$callReport) {
         $callCenterReport[$item->date][$item->callStatus] = $item->count;
    });

The $callReport result is :

{
  "2020-11-25": {
    "x": 74,
    "y": 172,
  },
  "2020-11-26": {
    "x": 10,
    "y": 49,
  }
}

But when i try with the collect methods :

        $callReport = collect();
        $callResult->each(function ($item) use (&$callReport ) {
            $callReport->push($item->date, collect([$item->callStatus => $item->count]));
        });

I can’t reach the same result :

[
  "2020-11-25",
  {
    "x": 74
  },
  "2020-11-25",
  {
    "y": 172
  },
  "2020-11-26",
  {
    "x": 10
  },
  "2020-11-26",
  {
    "y": 49
  }
]

What is wrong inside each while using collection methods?

Source: Laravel

Leave a Reply