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.

3 Answers 3

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;  
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I testes it and getting a "Declaration of App\Exports\ExcelExport::array() must be compatible with Maatwebsite\Excel\Concerns\FromArray::array(): array"
I think it's because of missing return type of array() function, Updated the answer
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?
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.
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.
|
0

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

Comments

-3

It Worked

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

with maatwebsite queue export.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.