alp-develop/laravel-livewire-tables
Reactive Livewire data tables for Laravel—search, sort, filter, paginate, export, and bulk actions with zero JavaScript. Supports Laravel 10–13, Livewire 3–4, PHP 8.1+, Tailwind or Bootstrap 4/5, plus dark mode and configurable themes.
Bulk actions let users select rows and perform operations on them. The system uses an exclusion model for efficient handling of large datasets.
Define the bulkActions() method in your table component:
public function bulkActions(): array
{
return [
'deleteSelected' => 'Delete Selected',
'exportSelected' => 'Export to CSV',
'activateSelected' => 'Activate',
];
}
Each key is the method name to call, and the value is the display label.
Define a public method for each action:
public function deleteSelected(): void
{
$ids = $this->getSelectedIds();
User::whereIn('id', $ids)->delete();
}
public function activateSelected(): void
{
$ids = $this->getSelectedIds();
User::whereIn('id', $ids)->update(['active' => true]);
}
After execution, deselectAll() is called automatically.
The HasExport trait provides automatic CSV export from visible columns without manual mapping:
public function bulkActions(): array
{
return [
'exportCsvAuto' => 'Export CSV',
];
}
That's it. exportCsvAuto() automatically:
resolveValue() methodblade, action, and image column typespublic function configure(): void
{
$this->setExportFilename('users'); // Default: 'export'
$this->setExportChunkSize(1000); // Default: 500
}
The filename is suffixed with the current date: users-2024-12-15.csv
For full control, build your own export method:
use Symfony\Component\HttpFoundation\StreamedResponse;
public function exportSelected(): StreamedResponse
{
$ids = $this->getSelectedIds();
$users = User::whereIn('id', $ids)->get();
return response()->streamDownload(function () use ($users) {
$handle = fopen('php://output', 'w');
fputcsv($handle, ['ID', 'Name', 'Email', 'Created']);
foreach ($users as $user) {
fputcsv($handle, [
$user->id,
$user->name,
$user->email,
$user->created_at->format('Y-m-d'),
]);
}
fclose($handle);
}, 'users-export.csv', ['Content-Type' => 'text/csv']);
}
Clicking individual checkboxes stores IDs in $selectedIds.
When "Select All" is clicked, the system switches to exclusion mode:
selectAllPages = true$excludedIdsThis means "all rows EXCEPT the excluded ones" — efficient even with millions of rows.
$ids = $this->getSelectedIds();
$selectedIds$excludedIds, respecting active search and filtersThe method uses the model's actual primary key (getKeyName()), so custom primary keys and UUIDs work correctly.
| Method | Description |
|---|---|
bulkActions() |
Define available actions |
getSelectedIds() |
Get resolved IDs (handles exclusion model) |
toggleSelected($id) |
Toggle a single row |
setPageSelection($ids, $select) |
Select/deselect all rows on current page |
selectAllAcrossPages() |
Enter exclusion mode (select everything) |
deselectAll() |
Clear all selections |
getSelectedCount($total) |
Get count of selected rows |
hasBulkActions() |
Check if any bulk actions are defined |
exportCsvAuto() |
Auto-generate CSV from visible columns |
setExportFilename(string) |
Set the export file name |
setExportChunkSize(int) |
Set the chunk size for streaming export |
bulkActions() returns actionsHow can I help you explore Laravel packages today?