PHP code example of alp-develop / laravel-livewire-tables
1. Go to this page and download the library: Download alp-develop/laravel-livewire-tables library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alp-develop / laravel-livewire-tables example snippets
'theme' => 'tailwind',
namespace App\Livewire\Tables;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Tables\Columns\TextColumn;
use Livewire\Tables\Columns\BooleanColumn;
use Livewire\Tables\Columns\ActionColumn;
use Livewire\Tables\Filters\SelectFilter;
use Livewire\Tables\Livewire\DataTableComponent;
class UsersTable extends DataTableComponent
{
public function configure(): void
{
$this->setDefaultPerPage(25);
$this->setSearchDebounce(300);
}
public function query(): Builder
{
return User::query();
}
public function columns(): array
{
return [
TextColumn::make('name')->sortable()->searchable(),
TextColumn::make('email')->sortable()->searchable(),
BooleanColumn::make('active')->sortable(),
TextColumn::make('created_at')
->label('Joined')->sortable()
->format(fn ($value) => $value?
->format('M d, Y')),
ActionColumn::make()
->button('Edit', fn ($row) => "edit({$row->id})", 'lt-btn-primary')
->button('Delete', fn ($row) => "delete({$row->id})", 'lt-btn-primary'),
];
}
public function filters(): array
{
return [
SelectFilter::make('active')
->label('Status')
->setOptions(['' => 'All', '1' => 'Active', '0' => 'Inactive'])
->filter(fn (Builder $q, $v) => $q->where('active', (bool) $v)),
];
}
public function bulkActions(): array
{
return [
'deleteSelected' => 'Delete Selected',
'exportCsvAuto' => 'Export CSV',
];
}
public function deleteSelected(): void
{
User::whereIn('id', $this->getSelectedIds())->delete();
}
public function edit(int $id): void
{
$this->redirect(route('users.edit', $id));
}
public function delete(int $id): void
{
User::findOrFail($id)->delete();
}
}
bash
php artisan make:livewiretable UsersTable User
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.