Laravel Excel Integration
Laravel Excel is the default package used when exporting DataTables to Excel and CSV.
Using Export Class
- 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
Example Export Class
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'], ]; }}