Reputation: 57
I'm trying to export a large amount of data with Laravel Excel Export, version 3.1, but I always have a lack of memory (my limit is 512M).
In the export class:
class ExcelExport implements FromQuery
{
use Exportable;
public function query()
{
return DB::table('myTable')->orderBy('date');
}
}
In the controller:
Excel::store(new ExcelExport(), 'myFile.xlsx');
From the official documentation I see the following:
"By using the FromQuery concern, we can prepare a query for an export. Behind the scenes this query is executed in chunks."
But it seems not to work as expected.
Is the use of Query Builder the problem?
Also, is there a way to set a chunk size?
I have tryied to use a limit clause in the query, like this:
public function query()
{
return DB::table('myTable')->orderBy('date')->limit(1000);
}
but it doesn't work: it seems that the limit is not used.
I have tryied to catch the error in a try...catch block:
try{
Excel::store(new ExcelExport(), 'myFile.xlsx');
}
catch(\Exception $e){
Log::error($e->getMessage());
}
but, still, it doesn't catch any exception: all that I see is a 500 internal server error.
Can someone help me with that?
Thank you very much in advance.
Upvotes: 0
Views: 13253
Reputation: 8719
You could try implicit export queueing.
use Illuminate\Contracts\Queue\ShouldQueue;
class ExcelExport implements FromQuery, ShouldQueue
{
use Exportable;
public function query()
{
return DB::table('myTable')->orderBy('date');
}
}
Call your export like this:
(new ExcelExport)->store('myFile.xlsx');
In this way multiple jobs will be chained in order to chunk the process.
Read more in the docs.
Upvotes: 1