Fast-Excel is recommended when exporting bulk records.
FastExcel integration uses cursor behind the scene thus eager loaded columns will not work on export. You MUST use join statements if you want to export related columns. Also, column value formatting like converting boolean to Yes or No should be done on SQL LEVEL.
- Install
fast-excel
usingcomposer require rap2hpoutre/fast-excel
. - Create a dataTable class
php artisan datatables:make Users
- Adjust
UsersDataTable
as needed. - Set property
$fastExcel = true
.
class UsersDataTable extends DataTable
{
protected $fastExcel = true;
...
}
- DataTables will now export csv & excel using
fast-excel
package.
- Just set property
$fastExcelCallback = false
. This is enabled by default for a better formatted output of exported file.
class UsersDataTable extends DataTable
{
protected $fastExcel = true;
protected $fastExcelCallback = false;
- Exported file will now be based on how your query was structured. No header formatting and all selected columns in sql will be included in the output.
Just override the fastExcelCallback
method:
class UsersDataTable extends DataTable
{
protected $fastExcel = true;
public function fastExcelCallback()
{
return function ($row) {
return [
'Name' => $row['name'],
'Email' => $row['email'],
];
};
}
...