jeudi 19 octobre 2017

Laravel avoid running out of memory

Im trying to use laravel excel to create an excel sheet. Because my database is expanding the memory limit is reached and I get the error:

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

I already raised the memory limit a few times but I would like to improve the function so it uses less memory so I dont need to raise the memory limit everytime. I already use chuncking to try and lower the memory using but to no avail.

My function:

public function exportExcel($year)
{

    $datum = date("d-m-Y");


    Excel::create('Sales export '.$datum, function($excel)use($year) {


        $datum = date("d-m-Y");

        // Chain the setters
        $excel->setCreator('author name')
            ->setCompany('company name')
            ->setDescription('sales export.')
            ->setTitle('sales export '.$datum);

        $excel->sheet('sales '.$datum, function($sheet)use ($year) {

            $sheet->appendRow(array(
                "merk","product","artikel nr","afbeelding","categorie","collectie","maat","omschrijving","inkoopprijs","verkoopprijs","prijs betaald","aantal verkocht","verkocht aan",  "totaal","dag","maand","jaar","kwartaal","reseller","verkoper","bestel naam", "status"
            ));
            Order::whereYear('created_at', '=', $year)->orderBy('billed_at','desc')->chunk(1, function($orders)use ($sheet){
                foreach($orders as $index => $order)
                {

                    foreach($order->products as $p)
                    {

                        $sizeLink = $p->productSize;
                        $productLink = $sizeLink->product;


                        // Append row as very last
                        $sheet->appendRow(array(
                            //Merknaam  Artikelnr.  Soort   Kleur   Maat
                            //Omschrijving  Geboekt aantal  Basiseenheid
                            //inkoopprijs   verkoopprijs    aant stuks verkocht
                            //Maand Jaar    Kwartaal


                            $productLink->brand->name, //merknaam
                            $productLink->name, //productnaam
                            $productLink->artnr, //Artikelnr
                            //link naar de hoofdafbeelding
                            "". URL::to('/'). ucfirst($productLink->mainthumb),
                            $productLink->category->name, //soort
                            $productLink->collection->name, //soort
                            $sizeLink->size->name,   //maat naam
                            $productLink->desciption,   //omschrijving
                            number_format((float) $productLink->price_buy_in, 2, ',', ''), //inkoopprijs
                            number_format((float) $productLink->price, 2, ',', ''), //verkoopprijs
                            number_format((float) $p->price, 2, ',', ''), //prijs betaald
                            $p->quantity, //geboekt aantal
                            $order->billingname . $order->billingnamelast, //verkocht aan
                            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->billed_at)), //dag
                            date("n",strtotime($order->billed_at)), //maand
                            date("Y",strtotime($order->billed_at)), //jaar
                            ceil(date("m",strtotime($order->billed_at))/3), // kwartaal
                            $order->reseller->name, // verkoper
                            $order->creator, // verkoper
                            $order->name, //text op factuur
                            $order->status->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');;
}

I know I could raise the memory limit but I would like to find out why so much memory is used and how I can avoid using to much memory.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire