Export an Excel file using Laravel-Excel 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?

&lt;?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-&gt;request = $request;
        $this-&gt;diid = $diid;
        $this-&gt;igid = $igid;
        $this-&gt;date = $date;
    }

    public function collection()
    {
        $dt = Carbon::parse($this-&gt;date);
        $endDt = Carbon::parse($this-&gt;date)-&gt;lastOfMonth();
        $period = CarbonPeriod::create($this-&gt;date, $endDt);
        $ipid = array_pluck($this-&gt;request, 'ip_id');
        $inidArr = array_pluck($this-&gt;request, 'in_id');
        $inid = array_unique($inidArr);
        $collection = [];
        foreach ($period as $key =&gt; $eachDate) {
            $reading = DB::table('instrument')
                    -&gt;where('iv_inid', $inid)
                    -&gt;whereIn('iv_ipid', $ipid)
                    -&gt;whereDate('iv_date', $eachDate)
                    -&gt;whereIn('iv_status', ['N', 'Y', 'A'])
                    -&gt;orderBy('iv_date')-&gt;get()-&gt;toArray();
            $row = array($eachDate-&gt;format('d/m/Y'));
            foreach ($reading as $columnKey =&gt; $columnValue) {
                array_push($row, $columnValue-&gt;iv_reading);
            }
            array_push($collection, $row);
        }

        return collect($collection);
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        $sheet_header = Arr::pluck($this-&gt;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?
The code looks OK. You may want to move the query outside of the foreach loop and group the results afterwards. But the foreach loop is limited to the number of days in a month, so I don't think that will do very much. That may cause the application to use more memory.

Therefore, I advise testing if the performance is indeed an issue. If so you might want to delegate creating the export to a queue. Queues in Laravel are not hard to implement (surprise!) 

Laravel Queues Documentation 7.x