yajra/laravel-datatables-export
Server-side export plugin for yajra/laravel-datatables using queues, Livewire, and OpenSpout. Adds an export-button component and queued batch jobs to generate spreadsheet exports from DataTable classes on Laravel 13 (PHP 8.3+).
composer require yajra/laravel-datatables-export:"^13.0"
php artisan queue:batches-table
php artisan migrate
php artisan vendor:publish --tag=datatables-export --force
<livewire:export-button :table-id="$dataTable->getTableId()" />
WithExportQueue:
use Yajra\DataTables\WithExportQueue;
class UsersDataTable extends DataTable
{
use WithExportQueue;
// Your DataTable methods...
}
php artisan queue:work
Queue-Based Export:
WithExportQueue trait to enable queued exports in your DataTable class.Livewire Integration:
<livewire:export-button> component handles UI interaction and job dispatching.Column Formatting:
Column::make('price')->exportFormat('0.00');
Column::make('date')->exportFormat('mm/dd/yyyy');
config/datatables-export.php).Dynamic Filenames:
<livewire:export-button :table-id="$dataTable->getTableId()" :filename="$customFilename" />
Custom Sheet Names:
Override sheetName() in your DataTable class:
protected function sheetName(): string
{
return "Custom Report - " . now()->format('Y-m');
}
Auto-Download: Force immediate download:
<livewire:export-button :table-id="$dataTable->getTableId()" auto-download="true" />
QueryBuilder Support: Works seamlessly with Eloquent or QueryBuilder queries.
Batch Processing:
Configure queue connection in config/datatables-export.php (v13.2.0+):
'queue_connection' => env('QUEUE_CONNECTION', 'database'),
yajra/laravel-datatables-buttons:
Required dependency for full functionality (e.g., buttons integration).storage/app/exports/ by default. Clean up with:
php artisan datatables:purge-export
Schedule this in app/Console/Kernel.php:
$schedule->command('datatables:purge-export')->weekly();
Queue Worker Required:
php artisan queue:work).Missing yajra/laravel-datatables-buttons:
composer require yajra/laravel-datatables-buttons:"^13.0"
PHP Version Mismatch:
Livewire Component Not Found:
resources/views/vendor/livewire/components/export-button.blade.php.php artisan vendor:publish --tag=datatables-export --force
Empty Values in Exports:
null. Use exportFormat('@') to force text:
Column::make('nullable_field')->exportFormat('@');
Check Queue Jobs:
Monitor jobs in .env database or Horizon (if installed):
php artisan queue:list
php artisan queue:work --once
Log Export Paths:
Temporarily log file paths in Yajra\DataTables\Export\ExportController to verify storage.
Validate Config:
Ensure config/datatables-export.php matches your Laravel version (e.g., queue_connection).
Custom Export Logic:
Override getExportData() in your DataTable class to modify exported data:
protected function getExportData()
{
return $this->query->with(['relationship'])->get();
}
Add New Export Types:
Extend Yajra\DataTables\Export\Export to support PDF/JSON exports.
Modify Storage:
Change the export directory in config/datatables-export.php:
'export_path' => storage_path('app/custom-exports'),
Livewire Event Hooks:
Listen to export events (e.g., export.started) via Livewire’s $dispatch:
protected $listen = ['export.started' => 'handleExportStart'];
chunk() in your query to avoid memory issues:
$this->query->chunk(1000, function ($rows) {
// Process rows...
});
QUEUE_WORKER_TIMEOUT in .env to prevent long-running jobs.OpenSpout vs. PHPSpreadsheet: The package defaults to OpenSpout (lightweight). For PHPSpreadsheet, install:
composer require phpoffice/phpspreadsheet
Then set in config:
'spreadsheet_driver' => 'PhpSpreadsheet',
Date Format Auto-Detection:
Ensure date columns use valid formats from config/datatables-export.php. Add custom formats as needed.
Livewire 3 vs. 4: If using Livewire 3, pin to v12.x of this package to avoid compatibility issues.
```markdown
### Pro Tips
- **Dynamic Filenames with Logic**:
Use Livewire properties to generate filenames dynamically:
```html
<livewire:export-button
:table-id="$dataTable->getTableId()"
:filename="'report-' . now()->format('Y-m-d') . '.xlsx'"
/>
Conditional Export Buttons: Show/hide buttons based on user roles:
@can('export-data')
<livewire:export-button :table-id="$dataTable->getTableId()" />
@endcan
Test Exports Locally:
Use php artisan queue:work --once to manually trigger exports during development.
Monitor Export Status:
Track job progress via Livewire’s $this->dispatch() or a separate status table.
How can I help you explore Laravel packages today?