Laravel Excel is the default package used when exporting DataTables to Excel and CSV.
- Create an export class
php artisan make:export UsersExport
- Update the generated export class and extend
DataTablesCollectionExport
namespace App\Exports;
use Yajra\DataTables\Exports\DataTablesCollectionExport;
class UsersExport extends DataTablesCollectionExport
{
}
- Update your
UsersDataTable
class and setprotected $exportClass = UsersExport::class
class UsersDataTable extends DataTable
{
protected $exportClass = UsersExport::class;
- Update your export class as needed. See official package docs: https://docs.laravel-excel.com/3.1/exports/collection.html
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithMapping;
use Yajra\DataTables\Exports\DataTablesCollectionExport;
class UsersExport extends DataTablesCollectionExport implements WithMapping
{
public function headings(): array
{
return [
'Name',
'Email',
];
}
public function map($row): array
{
return [
$row['name'],
$row['email'],
];
}
}