Laravel add to new collection from loop

178 Views Asked by At

I have the following piece of code :

    $callReport = array();
    $callResult->each(function ($item) use (&$callReport) {
         $callReport[$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
  }
]

I want to get the same result as the first one with the array using the collect methods. What is wrong in the second piece of code?

0

There are 0 best solutions below