我正在尝试将数据导出到an excel sheet但我收到错误:
Connection.php第321行中的FatalErrorException:允许的内存大小为 耗尽134217728个字节(尝试分配196605个字节)
我知道我可以提高php的内存限制,但我想了解为什么我的代码占用了这么多内存。
我的代码:
public function exportExcel()
{
$datum = date("d-m-Y");
Excel::create('Sales export '.$datum, function($excel) {
$datum = date("d-m-Y");
// Chain the setters
$excel->setCreator('some name')
->setCompany('some company')
->setDescription('sales export.')
->setTitle('Salesexport '.$datum);
$excel->sheet('sales '.$datum, function($sheet) {
$orders = Order::orderBy('created_at','desc')->get();
$sheet->appendRow(array(
"merk","product","artikel nr","categorie","collectie","maat","omschrijving","inkoopprijs","verkoopprijs","prijs betaald","aantal verkocht","verkocht aan", "totaal","dag","maand","jaar","kwartaal","reseller","verkoper","bestel naam"
));
foreach($orders as $order)
{
foreach($order->products as $p)
{
$sizeLink = $p->productSize;
$productLink = $sizeLink->product;
// Append row as very last
$sheet->appendRow(array(
$productLink->brand->name,
$productLink->name,
$productLink->artnr,
$productLink->category->name,
$productLink->collection->name,
$sizeLink->size->name,
$productLink->desciption,
number_format((float) $productLink->price_buy_in, 2, ',', ''),
number_format((float) $productLink->price, 2, ',', ''),
number_format((float) $p->price, 2, ',', ''),
$p->quantity, //geboekt aantal
$order->billingname . $order->billingnamelast,
number_format((float) $p->quantity * $p->price, 2, ',', ''), // totaal kosten
//number_format((float) ($p->quantity * $p->price - $p->quantity * $p->price_buy_in), 2, ',', ''), // winst inkoop-verkoop
date("d",strtotime($order->created_at)),
date("n",strtotime($order->created_at)),
date("Y",strtotime($order->created_at)),
ceil(date("m",strtotime($order->created_at))/3),
$order->reseller->name,
$order->creator,
$order->name,
));
}
}
// Auto filter for entire sheet
$sheet->setAutoFilter();
$sheet->freezeFirstRow();
// Set black background
$sheet->row(1, function($row) {
// call cell manipulation methods
$row->setBackground('#cccccc');
$row->setFontWeight("bold");
});
$sheet->setColumnFormat(array(
'G' => \PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00,
'H' => '[$EUR ]#,##0.00_-',
'I' => \PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00,
));
});
})->download('xlsx');;
}
答案 0 :(得分:1)
您似乎正在尝试同时处理过多的进程并占用大量内存,您应该使用Laravel集合的chunk()
方法:
Order::orderBy('created_at','desc')->chunk(10, function($orders)use ($sheet) {
foreach($orders as $index => $order) {
// Do your stuff here...
}
}
希望这有帮助!