I have this piece of code that is used to export an Excel file using Laravel-Excel. But I have doubts about it for the long run. Can the server handle it if there are a hundred requests?
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
class DailyGeneralSheetExport implements FromCollection, WithHeadings
{
use Exportable;
public function __construct(array $request, int $diid, int $igid, str $date)
{
$this->request = $request;
$this->diid = $diid;
$this->igid = $igid;
$this->date = $date;
}
public function collection()
{
$dt = Carbon::parse($this->date);
$endDt = Carbon::parse($this->date)->lastOfMonth();
$period = CarbonPeriod::create($this->date, $endDt);
$ipid = array_pluck($this->request, 'ip_id');
$inidArr = array_pluck($this->request, 'in_id');
$inid = array_unique($inidArr);
$collection = [];
foreach ($period as $key => $eachDate) {
$reading = DB::table('instrument')
->where('iv_inid', $inid)
->whereIn('iv_ipid', $ipid)
->whereDate('iv_date', $eachDate)
->whereIn('iv_status', ['N', 'Y', 'A'])
->orderBy('iv_date')->get()->toArray();
$row = array($eachDate->format('d/m/Y'));
foreach ($reading as $columnKey => $columnValue) {
array_push($row, $columnValue->iv_reading);
}
array_push($collection, $row);
}
return collect($collection);
}
/**
* @return array
*/
public function headings(): array
{
$sheet_header = Arr::pluck($this->request, 'ip_label');
return Arr::prepend($sheet_header, 'Date');
}
}
As you can see I have to create an array for one month and this makes me worry if this will impact the performance. And for some reason, I feel I wrote code that is not elegant as Laravel does. How do I optimize it?