3
votes

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.

3

3 Answers

2
votes

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;  
    }
}
0
votes

It Worked

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

with maatwebsite queue export.

0
votes

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