Hej,
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 ........................; }
class FeedBalanceDto { public $_date; public $_day; public $_qty; public $_water; public $_feed; public $_weight; public function __construct(http://www.php.net/array $options) { $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 }
public http://www.php.net/static function SumCollections($collection) { 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(); }
A dlaczego nie napiszesz sobie Query które wyciągnie Ci te dane zsumowane prosto z bazy i wrzucić sobie w DTO gotowe dane?
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)