Support the ongoing development of Laravel.io →
Laravel.io Laravel
Last updated by @mevadamayur26 11 months ago.
0

Here are my suggestions for improving the process for the user:

  • Use chunk instead of fetching all the records at once to keep memory usage low
  • Move the export logic to a background job
  • Also you can store the progress and show it somewhere on the frontend
  • Send an email or notification to the user initiating the export with the link to download the Excel file

Another possible solution can be adding indexing to the database, but as you are not filtering the data before export, I think the indexing will not improve the process.

Sometimes, there is time taking processes in the application, so we cannot do much of improvements there. Although by changing the processes as per the suggestions above, you can keep the user informed.

0

Thank you for suggestion

I want to pass that export file in apis response so for that I can not use this solution _Move the export logic to a background job _

I have implemented chunk, but still taking more time.

public function exportExcelWithChunk()
{
    $finance_details = Finance::query()
    ->with(['financeDetails' => function ($query) {
        $query->select('id', 'finance_id', 'description','rate', 'qty', 'uplift');
    }])
    ->take(100000)
    ->get();
    
    if (!empty($finance_details)) {
        foreach ($finance_details as $finance_value) {
            $grand_total = 0;
            foreach($finance_value->financeDetails as $finance_details_data)
            {
                $line_total = 0;
                $row_rate = $finance_details_data->rate ? $finance_details_data->rate : 0;
                $row_qty = $finance_details_data->qty ? $finance_details_data->qty : 0;
                $row_uplift = $finance_details_data->uplift ? $finance_details_data->uplift : 0;

                if (!empty($row_uplift)) {
                    $line_total = (((float) $row_rate + (((float) $row_rate * $row_uplift) / 100)) * $row_qty);
                } else {
                    $line_total = ((float) $row_rate * $row_qty);
                }
                $grand_total += $line_total;
            }
            $finance_arr = [];
            $finance_arr['job_number'] = $finance_value->job_id ? $finance_value->job_id : '';
            $finance_arr['doc_no'] = $finance_value->doc_no ? $finance_value->doc_no : '';
            $finance_arr['app_no'] = $finance_value->app_no ? $finance_value->app_no : '';
            $finance_arr['total_with_uplift'] = $grand_total;
            $finance_arr['finalised'] = $finance_value->is_final ? $finance_value->is_final : '';

            $collect_arr[] = $finance_arr;
        }
    }

  
    ini_set('max_execution_time',600);
    ini_set('memory_limit',"4096");



    Excel::create('Report', function ($excel) use ($collect_arr) {
        $excel->sheet('report', function ($sheet) use ($collect_arr) {
            $sheet->appendRow($this->columns());
            $query->chunk(1000, function ($rows) use ($sheet) {
                foreach ($rows as $row) {
                    $sheet->appendRow($this->rows($row));

        }
            });
        });
    })->download('xlsx');

}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.