Rodrigo Cabrera
Rodrigo Cabrera

Reputation: 107

Laravel Excel 3.1 export large data

I need to export over 100K records to Excel from database using Maatwebsite Laravel excel 3.1 plugin, the problem is that I get data as an array.

$data = $this->client->getData("sc/asistencia-social/informe",$request->all());

 return (new ExcelExport($data))->store('myFile.xlsx'); //using FromQuery

My ExcelExport Class :

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;

class ExcelExport implements FromQuery
{
 use Exportable, SerializesModels;
 private $data;

public function __construct($data)
{   
   $this->data = $data;     //Inject data 
}

public function query()
{   
   return $this->data;  
}
}

Actually, I get a "Call to a member function chunk() on array" error. I even tried to convert it into a collection with no success. Is there any possible solution to this.

Upvotes: 4

Views: 7928

Answers (3)

Sam
Sam

Reputation: 51

You should use shouldQueue to export large data to an Excel file.

Upvotes: 0

Anmol Mourya
Anmol Mourya

Reputation: 550

It Worked

In Query remove ->get() or replace with ->paginate(100)

with maatwebsite queue export.

Upvotes: -3

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

You have created your export class as FromQuery export class. instead create a FromArray export class.

Note the Implement interface and the function name

class ExcelExport implements FromArray // this was FromQuery before
{
    use Exportable, SerializesModels;

    private $data;

    public function __construct($data)
    {   
        $this->data = $data;     //Inject data 
    }

    public function array(): array // this was query() before
    {   
        return $this->data;  
    }
}

Upvotes: 2

Related Questions