skip to Main Content

Is there a workaround to make large file exports work with Laravel-Excel (version 3.1)? All posts I saw here and on GitHub are old with no real solutions. The only solution is to increase memory_limit in php.ini. But I already have it at 2048M.

Assuming I am using a collection:

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return Invoice::all();
    }
}

Is there any way to make it work? WithChunkReading is only for imports, and does not work for exports: https://docs.laravel-excel.com/3.1/imports/chunk-reading.html#chunk-reading

2

Answers


  1. You can try to implement FromQuery concern. Instead of FromCollection. In this way behind the scenes query is executed in chunks.

    Try the code and see if it brings any difference.

    use MaatwebsiteExcelConcernsFromQuery;
    
    class InvoicesExport implements FromQuery, WithChunkReading
      public function query()
       {
        return Invoice::query();
       }
    
      public function chunkSize(): int
       {
        return 200;
       }
    

    Make sure to not use ->get() the result.

    For reference you can look it here.

    Login or Signup to reply.
  2. I advise you to try library FastExcelLaravel, it works very fast and uses a minimum of memory. And you don’t need to create additional classes to export models.

    // Create workbook with sheet named 'Invoices'
    $excel = Excel::create('Invoices');
    
    // Export all users to Excel file
    $sheet->exportModel(Invoices::class);
    
    // Save Excel file
    $excel->saveTo('path/file.xlsx');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search