Installation:
composer require amirami/livewire-datatables
php artisan vendor:publish --provider="Amirami\LivewireDataTables\LivewireDataTablesServiceProvider" --tag="livewire-datatables-config"
Publish the config to customize default settings (e.g., datatables.php).
Basic Setup:
Amirami\LivewireDataTables\Traits\LivewireDataTable in your Livewire component.getData() method to return a query builder instance or collection:
use Amirami\LivewireDataTables\Traits\LivewireDataTable;
class UserDataTable extends Component
{
use LivewireDataTable;
public function getData()
{
return User::query();
}
}
<livewire:user-data-table />
First Use Case: Display a paginated table with search and sorting:
// In your component
public function configure()
{
return $this->setPrimaryKey('id')
->setSearchColumn('name')
->setPerPage(10);
}
Query Building:
getData() to return a query builder or collection. The package handles pagination, search, and sorting automatically.getQuery():
public function getQuery()
{
return $this->getData()->where('active', true);
}
Column Configuration:
getColumns():
public function getColumns()
{
return [
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
];
}
getColumnFields():
public function getColumnFields()
{
return [
'name' => 'name',
'email' => function ($value) {
return Str::limit($value, 20);
},
];
}
Actions and Buttons:
getActions():
public function getActions()
{
return [
'edit' => 'Edit',
'delete' => 'Delete',
];
}
getActionButtons():
public function getActionButtons()
{
return [
'edit' => function ($row) {
return '<a href="/users/' . $row->id . '/edit">Edit</a>';
},
'delete' => function ($row) {
return '<button wire:click="delete(' . $row->id . ')" class="btn btn-danger">Delete</button>';
},
];
}
Modular Components:
UserDataTable, OrderDataTable) by reusing the trait.Integration with Livewire:
public function delete($id)
{
User::find($id)->delete();
$this->dispatchBrowserEvent('table-updated');
}
Styling:
<div class="table-responsive">
<livewire:user-data-table />
</div>
API Integration:
getData():
public function getData()
{
return collect(Http::get('https://api.example.com/users')->json());
}
Relationships:
getData():
public function getData()
{
return User::with('roles')->query();
}
public function getColumnFields()
{
return [
'role' => function ($value) {
return $value->roles->first()->name ?? 'None';
},
];
}
Custom Views:
php artisan vendor:publish --provider="Amirami\LivewireDataTables\LivewireDataTablesServiceProvider" --tag="livewire-datatables-views"
resources/views/vendor/livewire-datatables/table.blade.php.Localization:
'labels' => [
'no_results' => 'No records found.',
],
Query Caching:
getData() if using real-time updates. The package re-executes the query on each request.private $users;
public function getData() {
if (!$this->users) {
$this->users = User::all();
}
return $this->users;
}
Primary Key Conflicts:
setPrimaryKey() matches the actual primary key of your model. Conflicts may cause silent failures in row selection.Column Mismatches:
getColumns() and getColumnFields() have mismatched keys, the table may render incorrectly or throw errors.getColumnFields() against getColumns().Livewire Hooks:
mount() or hydrate() may interfere with the package's initialization. Avoid modifying these unless necessary.Pagination Conflicts:
public function configure()
{
return $this->disablePagination();
}
Query Logs:
DB::enableQueryLog();
// After table renders
dd(DB::getQueryLog());
Wire:Debug:
@livewireScripts
<script>
window.addEventListener('livewire:init', () => {
Livewire.debug = true;
});
</script>
Event Listeners:
livewire-data-table-updated events to debug real-time changes:
window.addEventListener('livewire-data-table-updated', (event) => {
console.log('Table updated:', event.detail);
});
Performance:
select() to limit columns in getData():
public function getData()
{
return User::query()->select('id', 'name', 'email');
}
Conditional Logic:
public function getColumns()
{
$columns = ['id', 'name'];
if (auth()->user()->isAdmin) {
$columns[] = 'email';
}
return $columns;
}
Reusability:
class BaseDataTable extends Component
{
use LivewireDataTable;
public function configure()
{
return $this->setPerPage(20)->setSearchColumn('name');
}
}
Testing:
Livewire::test():
public function test_table_renders()
{
Livewire::test(UserDataTable::class)
->assertSee('User Table')
->assertStatus(200);
}
Extensions:
trait ExportableDataTable
{
public function export()
{
return Excel::download(new UserExport($this->getData()), 'users.xlsx');
}
}
use ExportableDataTable;
class UserDataTable extends Component
{
use LivewireDataTable, ExportableDataTable;
}
Config Overrides:
config/datatables.php:
'default_per_page' => 50,
'enable_search' => false,
Hidden Columns:
public function getColumns()
{
return [
'id' => 'ID',
'hidden_column' => 'Hidden',
];
}
public function getColumn
How can I help you explore Laravel packages today?