poniżej mój testowy kontroler
public function feedBalance($id) { $demandList = collect(); $document = $this->documentRepository->get($id); foreach ($document->details as $detail) { $demandList->push($this->foodSupplyService->getFeedBalance($detail)); } return view ........................; }
metoda getFeedBalance zwraca mi kolekcję takiego dto
class FeedBalanceDto { public $_date; public $_day; public $_qty; public $_water; public $_feed; public $_weight; { $this->_date = $options['date']; $this->_day = $options['day']; $this->_qty = $options['quantity']; $this->_water = $options['water']; $this->_feed = $options['feed']; $this->_weight= $options['weight']; } //gettery }
Collection {#488 ▼ #items: array:1 [▼ 0 => Collection {#492 ▼ #items: array:115 [▶] } ] }
0 => FeedBalanceDto {#772 ▼ +_date: "2019-06-27" +_day: "26" +_qty: "4998" +_water: 879.648 +_feed: 494802 +_weight: 4548180 }
Jaki jest mój cel: potrzebuję scalić wszystkie dto w jeden w ramach atrybutu "date" tzn sumować poszczególne klucze grupując po dacie.
Rezultatem miałaby być kolekcja DTO ale już zagregowanych co do date...
moje próby jak na razie kończą się na tym
narazie kończe na tym co nie daje rezultatu
{ return collect($collection->first())->keys()->mapWithKeys(function($item,$key) use($collection){ return[ $item => $collection->map(function ($mapItem, $mapKey) use($item) { return $mapItem[$item]; }) ]; })->mapWithKeys(function($item,$key){ $eachLine = collect($item->first())->keys()->mapWithKeys(function($mapItem) use($item){ return[ $mapItem => $item->sum($mapItem) ]; }); return [$key => $eachLine]; })->all(); }
może powinienem jakoś lepiej przetrzymywać te dane aby łatwiej zsumować?
help...
p.s. _date w DTO zawsze leci od daty x + `1 dzień. Oczywiście 1sza data w każdym dto może być różna.