yajra/laravel-datatables-export
Queue-based server-side exports for yajra/laravel-datatables using Livewire and OpenSpout. Adds an export-button component and WithExportQueue trait to DataTable classes, enabling scalable Excel/CSV-style exports via Laravel batch jobs.
## Getting Started
### Minimal Setup
1. **Install the package**:
```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
For a basic DataTable with export functionality:
use Yajra\DataTables\WithExportQueue;
class UsersDataTable extends DataTable
{
use WithExportQueue;
// ... rest of your DataTable logic
}
<livewire:export-button :table-id="$dataTable->getTableId()" />
php artisan queue:work
DataTable Integration:
WithExportQueue trait in your DataTable class to enable queued exports.Yajra\DataTables\DataTable (or Yajra\DataTables\EloquentDataTable for Eloquent).Livewire Button Placement:
<livewire:export-button /> near your DataTable HTML element.:table-id="$dataTable->getTableId()").Queue Processing:
storage/app/exports/ by default (configurable).Dynamic Filename:
<livewire:export-button
:table-id="$dataTable->getTableId()"
:filename="'users-' . now()->format('Y-m-d') . '.xlsx'"
/>
Custom Export Types:
<!-- CSV Export -->
<livewire:export-button
:table-id="$dataTable->getTableId()"
type="csv"
buttonName="Export as CSV"
/>
<!-- Excel Export -->
<livewire:export-button
:table-id="$dataTable->getTableId()"
type="xlsx"
sheet-name="User Records"
/>
Auto-Download:
<livewire:export-button
:table-id="$dataTable->getTableId()"
auto-download="true"
/>
Column Formatting:
// In your DataTable class
$this->addColumn('price', function ($row) {
return '$' . number_format($row->price, 2);
})->exportFormat('0.00');
Date Handling:
$this->addColumn('created_at', function ($row) {
return $row->created_at->format('Y-m-d H:i:s');
})->exportFormat('mm/dd/yyyy hh:mm:ss');
Custom Export Logic:
Override getExportData() in your DataTable:
protected function getExportData()
{
return $this->collection->map(function ($row) {
return [
'name' => $row->name,
'custom_field' => $this->formatCustomField($row),
];
});
}
Batch Processing: For large datasets, use chunking:
use Yajra\DataTables\WithBatchProcessing;
class LargeDataTable extends DataTable
{
use WithExportQueue, WithBatchProcessing;
// ...
}
Post-Export Actions:
Listen to export events in your EventServiceProvider:
protected $listen = [
'Yajra\DataTables\Events\ExportStarted' => [
'App\Listeners\LogExportStart',
],
'Yajra\DataTables\Events\ExportCompleted' => [
'App\Listeners\NotifyExportComplete',
],
];
Queue Worker Required:
php artisan queue:work).File Storage Path:
storage/app/exports/.config/datatables-export.php:
'export_path' => storage_path('app/exports/custom_path'),
Livewire Version:
composer.json).yajra/laravel-datatables-export:^12.3.PHP Version:
yajra/laravel-datatables-export:^12.3.Empty Values:
null in exports. Use ->exportFormat('@') to force text:
Column::make('nullable_field')->exportFormat('@');
Date Formats:
config/datatables-export.php).Y-m-d may not work; use mm/dd/yyyy or NumberFormat::FORMAT_DATE_YYYYMMDD.Large Files:
use WithBatchProcessing;
php artisan queue:failed and retry as needed.Button Visibility:
getTableId(). Ensure this is unique per table instance.Queue Logs:
storage/logs/laravel.log for export job failures.tail -f storage/logs/laravel.log
Export Job Inspection:
php artisan queue:list
php artisan queue:work --once --verbose
File Permissions:
chmod -R 755 storage/app/exports
Configuration Overrides:
php artisan vendor:publish --tag=datatables-export-config
'export_path' => 'custom/path',
'disk' => 's3', // For cloud storage
'auto_download' => true,
Custom Export Formats:
Extend Yajra\DataTables\Formatters\NumberFormat for new formats:
class CustomNumberFormat extends NumberFormat
{
const FORMAT_CUSTOM = 'custom_format';
public static function getFormat(self::FORMAT_CUSTOM)
{
return ['type' => self::TYPE_NUMBER, 'formatCode' => '#,##0.00;(#,##0.00)'];
}
}
Pre/Post Export Hooks: Use events to modify export behavior:
// app/Providers/EventServiceProvider.php
protected $listen = [
'Yajra\DataTables\Events\ExportStarted' => [
'App\Listeners\ModifyExportData',
],
];
Storage Backends: Customize storage using Laravel’s filesystem:
// config/datatables-export.php
'disk' => 's3',
'export_path' => 'exports',
Livewire Component Extensions:
Override ExportButton to add custom logic:
namespace App\Livewire;
use Yajra\DataTables\Livewire\ExportButton as BaseExportButton;
class CustomExportButton extends BaseExportButton
{
public function mount($tableId, $type = 'xlsx', $filename = null)
{
$this->filename = $filename ?? 'custom_' . $this->tableId . '.' . $type;
// Custom logic here
}
}
Queue Chunking: For very large exports, implement chunked processing:
use Yajra\DataTables\WithBatchProcessing;
class LargeExportDataTable extends DataTable
{
use WithExportQueue, WithBatchProcessing;
public function __construct()
{
$this->batchSize = 1000; // Process 10
How can I help you explore Laravel packages today?