Today I want to briefly overview one package Laravel-Excel. I probably don't need to explain its purpose - the title says everything. But what I do want to show you is how easy it is to export data to Excel using Eloquent.
Basically, Laravel-Excel package is using PHPExcel under the hood, just re-purposing it for Laravel specifically.
Ok, boring part. Usual stuff:
When installing, we see the dependencies visually:
'Excel' => Maatwebsite\Excel\Facades\Excel::class
3. Publishing the config:
php artisan vendor:publish
Now, the magic
Let's say we have a usual users table - the default one from Laravel installation. And let's say we want to export that to Excel - but only some fields (we don't want to expose passwords, even hashed, do we?).
The result is a downloaded file users.xls (filename comes from the parameter Excel::create('users' ...), which looks like this:
Sweet, isn't it?
Want more articles like this every week? Subscribe!
Of course, Laravel-Excel has much more functions. You can style your sheets, also import data into your database from Excel and do various other stuff.
Keep in mind that there are some requirements for the package to work properly - here's the list from the docs:
PHP version >= 5.3.7
Laravel >= 4.1
PHPOffice PHPExcel >= 1.8.0 (included by composer.json)
PHP extension php_zip enabled (required if you need PHPExcel to handle .xlsx .ods or .gnumeric files)
PHP extension php_xml enabled
PHP extension php_gd2 enabled (optional, but required for exact column width autocalculation)