4

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.

Rodrigo Cabrera
  • 81
  • 2
  • 10

3 Answers3

2

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;  
    }
}
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
  • I testes it and getting a "Declaration of App\Exports\ExcelExport::array() must be compatible with Maatwebsite\Excel\Concerns\FromArray::array(): array" – Rodrigo Cabrera May 02 '19 at 15:09
  • I think it's because of missing return type of `array()` function, Updated the answer – Tharaka Dilshan May 02 '19 at 15:12
  • It's still running, it's taking more than 4 minutes to export aprox 30k records, I read the FromQuery leverages chunks,some say only on import but no export. is that so? – Rodrigo Cabrera May 02 '19 at 15:55
  • 1
    No, you __CAN__ use FromQuery to export too, and its better than this approach too. But if you want to do that, you don't want to pass data through the constructor. you need to write database query in the `query()` function. If there are lot of data in the database I think thats the best solution. – Tharaka Dilshan May 02 '19 at 15:58
  • ok, in my case, $data = $this->client->getData("sc/asistencia-social/informe",$request->all()); send a request to the API to query the database, the system doesn't make use of ORM only plain sql with parameters like a range of dates, is it still possible? – Rodrigo Cabrera May 02 '19 at 19:00
  • 1
    Not possible. If you wanna use `FromQuery` approach you have to provide a sql query to that `query()` function. Then they will take care of fetching data from the database (there's where they apply chunks). Otherwise you can't use `FromQuery` approach. – Tharaka Dilshan May 03 '19 at 00:41
0

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

KazikM
  • 1,257
  • 5
  • 21
  • 29
Sam
  • 51
  • 1
  • 2
-3

It Worked

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

with maatwebsite queue export.

Anmol Mourya
  • 530
  • 5
  • 7