items as $item) { if (!isset($taxes[$item->tax])) { $taxes[$item->tax] = ['tax' => 0, 'taxable' => 0]; } $taxes[$item->tax]['tax'] += round($item->price * $item->amount * $item->tax / 100, 2, PHP_ROUND_HALF_UP); $taxes[$item->tax]['taxable'] += $item->price * $item->amount; } return response()->streamDownload(function () use ($invoice, $taxes) { echo view('xml.invoice', ['invoice' => $invoice, 'options' => Option::optionsAsObject(), 'taxes' => $taxes]); }, 'test.xml', ['Content-Type' => 'application/xml']); } }