yajra/laravel-datatables-export
Laravel DataTables export plugin for server-side exporting via queued jobs, OpenSpout, and Livewire. Adds an export button component and DataTable trait (WithExportQueue) to generate Excel/CSV exports for jQuery DataTables 2.x on Laravel 13.
## Getting Started
### Minimal Setup
1. **Install the package** in your Laravel 13.x project:
```bash
composer require yajra/laravel-datatables-export:"^13.0"
php artisan queue:batches-table
php artisan migrate
php artisan vendor:publish --tag=datatables-export --force
WithExportQueue:
use Yajra\DataTables\WithExportQueue;
class UsersDataTable extends DataTable
{
use WithExportQueue;
// Your DataTable methods...
}
<livewire:export-button :table-id="$dataTable->getTableId()" />
php artisan queue:work
Queue-Based Export:
storage/app/export/.Livewire Integration:
<livewire:export-button> component handles UI state (e.g., loading/spinner).filename, type (csv/xlsx), and auto-download.DataTable Customization:
Column::make() to define export formats:
Column::make('price')->exportFormat('0.00'),
Column::make('date')->exportFormat('mm/dd/yyyy'),
<livewire:export-button
:table-id="$dataTable->getTableId()"
:filename="'users_' . now()->format('Y-m-d') . '.xlsx'"
/>
@if(auth()->user()->can('export-users'))
<livewire:export-button :table-id="$dataTable->getTableId()" />
@endif
sheetName() in your DataTable:
protected function sheetName(): string
{
return "Active Users - " . now()->format('Y');
}
Queue Configuration:
queue:work --daemon for production.php artisan queue:failed and retry with php artisan queue:retry.File Cleanup:
app/Console/Kernel.php:
$schedule->command('datatables:purge-export')->weekly();
Custom Export Logic:
ExportQueue class to add pre/post-processing:
use Yajra\DataTables\ExportQueue;
class CustomExportQueue extends ExportQueue
{
protected function beforeExport()
{
// Add custom logic (e.g., headers, footers)
}
}
Queue Worker Required:
php artisan queue:work).QUEUE_CONNECTION=database in .env for testing (but avoid in production).File Storage Paths:
storage/app/export/. Ensure this directory is writable.FILESYSTEM_DISK=s3 in .env and verify the tmp_path setting in datatables-export.php.Livewire Version Mismatch:
Date Format Issues:
dd/mm/yyyy not in date_formats config) will default to raw values.NumberFormat constants for consistency:
Column::make('created_at')->exportFormat(NumberFormat::FORMAT_DATE_DATETIME),
Empty Values:
null values may be replaced with 0 or null in exports. Use exportFormat('@') to force text:
Column::make('notes')->exportFormat('@'),
Check Queue Jobs:
php artisan queue:failed
tail -f storage/logs/laravel.log
Verify Export Paths:
export() method:
public function export()
{
\Log::info('Export path:', [$this->getExportPath()]);
return parent::export();
}
Test with Small Datasets:
->limit(10) in your DataTable query to debug formatting issues without overwhelming the queue.Custom Export Formats:
Yajra\DataTables\Formatters\NumberFormat to add new formats:
class CustomNumberFormat extends NumberFormat
{
const FORMAT_CUSTOM = 'custom_format';
// ...
}
Override Export Logic:
Yajra\DataTables\ExportQueue to modify the export process:
class CustomExportQueue extends ExportQueue
{
protected function getWriter()
{
// Customize writer logic (e.g., add encryption)
return parent::getWriter();
}
}
Add New Export Types:
ExportQueue and integrating a library like barryvdh/laravel-dompdf.OpenSpout vs. PHPSpreadsheet:
'export' => [
'driver' => 'phpspreadsheet',
],
phpoffice/phpspreadsheet (^1.25).Date Format Overrides:
date_formats in config/datatables-export.php to support your preferred formats:
'date_formats' => [
'your-custom-format',
// ...
],
Auto-Download Behavior:
auto-download="true", the browser triggers a download immediately after the queue job completes.Batch Processing:
$this->query->chunk(1000, function ($rows) {
// Process rows in batches
});
Queue Priorities:
<livewire:export-button :table-id="$dataTable->getTableId()" priority="high" />
Memory Limits:
.env for large exports:
MEMORY_LIMIT=4G
---
How can I help you explore Laravel packages today?