This post introduce FastExcel for Laravel, gives an expeditious overview over PHP engenderers and determinately shows how to utilize both to engender Excel files from amassments while preserving recollection.
About FastExcel
Laravel FastExcel is intended at being Laravel-flavoured Spout, with the goal of simplifying imports and exports. It could be considered as a more expeditious (and recollection cordial) alternative to Laravel Excel with a different approach and less features. Getting commenced in 2 steps.
Install with composer:
composer require rap2hpoutre/fast-excel
Then export a Model or a Collection to XLSX
, CSV
or ODS
:
fastexcel($collection)->export('file.xlsx');
More information on the README
of the project homepage.
Generators
Generators were introduced in PHP 5, many years ago. A generator function looks just like a normal function, except that instead of returning a value, a generator yields values. Then you can iterate over the generator function.
On of the goal of such a function is to lazy iterate over data without building an array. So it preserves memory when manipulating large datasets. Quoting PHP documentation:
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.
Assuming you have a User
model with 10M+ entries in database and you want to iterate over it in your code, instead of just calling User::all()
, you could use a generator:
function usersGenerator() {
foreach (User::cursor() as $user) {
yield $user;
}
}
$users = usersGenerator();
foreach($users as $user) {
// Do something with each user without hitting memory limit
}
The example above runs queries to only take users one by one. It only uses the memory needed to load 1 user N
times.
Export large dataset using FastExcel and Generators
Since v1.3.0, FastExcel accepts generator function as a parameter. Using the previous example, you could just pass the generator to fastexcel
function:
function usersGenerator() {
foreach (User::cursor() as $user) {
yield $user;
}
}
// Export consumes only a few MB, even with 10M+ rows.
$users = usersGenerator();
fastexcel($users)->export('test.xlsx');
FastExcel internally creates rows one by one when using generator, so it will not consume extra memory. It will be a long process, so make sure it does not hit max_execution_time
(you could use Queue, any asynchronous technique, increase max execution time, or even run it from CLI). Still, your server will be grateful to not consume all its memory for one export.
So thanks to generators, you can now export thousands of models to XLSX
, CSV
and ODS
files, with a few lines of code in a Laravel project.
Learn more about he library in the repository: https://github.com/rap2hpoutre/fast-excel