2016-11-14 18 views
0

我試着去我的數據導出到an excel sheet,但我得到的錯誤:Laravel了內存

FatalErrorException in Connection.php line 321: Allowed memory size of 134217728 bytes exhausted (tried to allocate 196605 bytes)

我明白我可以只了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

你通常在這裏處理多少行? –

+0

訂單containts 135行。訂購產品1350 –

+0

您可以將數據存儲在緩存中(每天週三凌晨3點),然後嘗試導出它們或者您需要「新鮮」數據? –

回答

1

看來你要處理在其正在採取這麼多的存儲器的同時過多的進程,你應該使用Laravel的收藏chunk()方法是這樣的:

Order::orderBy('created_at','desc')->chunk(10, function($orders)use ($sheet) { 
    foreach($orders as $index => $order) { 
     // Do your stuff here... 
    } 
} 

希望這有助於!

+0

將塊進行限制查詢,或者只是將array_chunk應用於結果集? –

+0

我得到一行錯誤:foreach($ order-> products-> chunk(10)as $ p) {說:「未定義的屬性:Illuminate \ Database \ Eloquent \ Collection :: $ products」 –

+0

'chunk )'方法處理代碼塊,這樣php一次處理10個集合,而不是1350個集合(正如你所提到的那樣),它可以避免大量的內存使用。 –