Here are my suggestions for improving the process for the user:
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.
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');
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community