dndarksan/laravel-livewire-datatable-only-arrays
Livewire v2 + Bootstrap 4 datatable component for arrays only. Install via Composer and generate tables with php artisan make:dt-table. Define headers, columns, and records arrays to enable sortable/searchable columns and per-cell classes.
Installation:
composer require dndarksan/laravel-livewire-datatable-only-arrays
Ensure Livewire v2 is already installed and configured in your Laravel project.
Generate a Datatable Component:
php artisan make:dt-table UserDatatable
This creates a Livewire component (UserDatatable) in /app/Http/Livewire.
Render the Component:
Add @livewire('user-datatable') to your Blade view.
Create a simple datatable for a users table:
encabezados()):
public function encabezados(): array
{
return [
[
"name" => "ID",
"campo" => "id",
"sorteable" => true,
],
[
"name" => "Name",
"campo" => "name",
"searchable" => true,
],
];
}
datos()):
public function datos(): array
{
return User::all()->toArray();
}
@livewire('user-datatable')
Component Generation:
Use make:dt-table to scaffold a new datatable component with pre-configured methods (encabezados, datos, etc.).
Data Binding:
name, campo, sorteable, searchable).toArray() for Eloquent results.
public function datos(): array
{
return User::query()
->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
->get()
->toArray();
}
Sorting/Searching:
"sorteable" => true and specifying "campo"."searchable" => true and handle the search logic in datos():
public $search = '';
public function updatingSearch()
{
$this->resetPage();
}
Pagination: Use Livewire’s built-in pagination:
public function datos(): array
{
return User::paginate(10)->toArray();
}
Add pagination controls to your Blade view:
{{ $this->withPagination() }}
Styling: The package uses Bootstrap 4 by default. Customize via CSS or override the provided Blade template.
API Integration: Fetch data from external APIs and transform responses into arrays:
public function datos(): array
{
$response = Http::get('https://api.example.com/users');
return $response->json();
}
Dynamic Columns: Load headers dynamically based on user roles or permissions:
public function encabezados(): array
{
if (auth()->user()->isAdmin) {
return [...]; // Admin columns
}
return [...]; // Default columns
}
Reusable Components: Extend the base component for shared functionality:
class BaseDatatable extends DatatableOnlyArrays
{
public function getDefaultHeaders(): array { ... }
}
Data Format:
datos(). Eloquent collections or objects will break the table.Sorting/Searching:
"campo" in headers disables sorting/searching.strtolower() in search logic if needed:
->where('name', 'like', "%{$this->search}%")
Pagination:
paginate() instead of simplePaginate() for consistency.Blade Template Overrides: The package assumes a default Blade template. Override it by publishing assets:
php artisan vendor:publish --tag=livewire-datatable-only-arrays
Then modify /resources/views/vendor/livewire-datatable-only-arrays/....
Check Headers:
Dump encabezados() to verify structure:
dd($this->encabezados());
Inspect Data:
Log datos() output to ensure correct array format:
\Log::debug('Datatable data:', $this->datos());
Livewire Hooks:
Use mount() or hydrate() to debug component initialization:
public function mount()
{
\Log::info('Component mounted');
}
Performance:
cursor() for large datasets to avoid memory issues:
public function datos(): array
{
return User::cursor()->get()->toArray();
}
loadMore events.Localization: Customize labels (e.g., "No results") by overriding the Blade template.
Extensions: Add custom actions (e.g., buttons) by extending the Blade template:
<td>
<button wire:click="edit({{ $item['id'] }})">Edit</button>
</td>
Testing: Test components with Livewire’s testing helpers:
$this->livewire(UserDatatable::class)
->assertSee('User Table')
->assertDontSee('Error');
How can I help you explore Laravel packages today?