1. Go to this page and download the library: Download arm092/livewire-datatables 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/ */
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Arm092\LivewireDatatables\DateColumn;
use Arm092\LivewireDatatables\LabelColumn;
use Arm092\LivewireDatatables\NumberColumn;
use Arm092\LivewireDatatables\BooleanColumn;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class ComplexDemoTable extends LivewireDatatable
{
public function builder(): Builder
{
return User::query();
}
public function getColumns(): array|Model
{
return [
NumberColumn::name('id')
->label('ID')
->linkTo('job', 6),
BooleanColumn::name('email_verified_at')
->label('Email Verified')
->format()
->filterable(),
Column::name('name')
->defaultSort('asc')
->group('group1')
->searchable()
->hideable()
->filterable(),
Column::name('planet.name')
->label('Planet')
->group('group1')
->searchable()
->hideable()
->filterable($this->planets),
// Column that counts every line from 1 upwards, independent of content
Column::index($this);
DateColumn::name('dob')
->label('DOB')
->group('group2')
->filterable()
->hide(),
(new LabelColumn())
->label('My custom heading')
->content('This fixed string appears in every row'),
NumberColumn::name('dollars_spent')
->format(2, '.', ',') // 2 decimal places, comma for thousands, dot for decimal
->enableSummary(),
];
}
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class GroupDemoTable extends LivewireDatatable
{
public array $groupLabels = [
'group1' => 'app.translation_for_group_1',
'group2' => 'app.translation_for_group_2',
];
public function getColumns(): array|Model
{
return [
Column::name('planets.name')
->group('group1')
->label('Planet'),
Column::name('planets.name')
->group('group2')
->label('Planet'),
public function getColumns(): array|Model
{
return [
Column::name('dollars_spent')
->label('Expenses in Dollar')
->enableSummary(),
Column::name('euro_spent')
->label('Expenses in Euro')
->enableSummary(),
public function buildActions(): array
{
return [
Action::value('edit')->label('Edit Selected')->group('Default Options')->callback(function ($mode, $items) {
// $items contains an array with the primary keys of the selected items
}),
Action::value('update')->label('Update Selected')->group('Default Options')->callback(function ($mode, $items) {
// $items contains an array with the primary keys of the selected items
}),
Action::groupBy('Export Options', function () {
return [
Action::value('csv')->label('Export CSV')->export('SalesOrders.csv'),
Action::value('html')->label('Export HTML')->export('SalesOrders.html'),
Action::value('xlsx')->label('Export XLSX')->export('SalesOrders.xlsx')->styles($this->exportStyles)->widths($this->exportWidths)
];
}),
];
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Arm092\LivewireDatatables\Traits\CanPinRecords;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class RecordTable extends LivewireDatatable
{
use CanPinRecords;
public string|null|Model $model = Record::class;
public function getColumns(): array|Model
{
return [
Column::checkbox(),
// ...
public function builder(): Builder
{
return User::query()
->leftJoin('planets', 'planets.id', 'users.planet_id')
->leftJoin('moons', 'moons.id', 'planets.moon_id')
->groupBy('users.id');
}
public function getColumns(): array|Model
{
return [
NumberColumn::name('id')
->filterable(),
Column::name('planets.name')
->label('Planet'),
Column::raw('GROUP_CONCAT(planets.name SEPARATOR " | ") AS `Moon`')
->setCallback(static fn($value) => strtolower($value))
...
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class CallbackDemoTable extends LivewireDatatable
{
public string|null|Model $model = User::class
public function getColumns(): array|Model
{
return [
Column::name('users.id'),
Column::name('users.dob')->format(),
Column::callback(['dob', 'signup_date'], function ($dob, $signupDate) {
$age = $signupDate->diffInYears($dob);
return $age > 18
? '<span class="text-red-500">' . $age . '</span>'
: $age;
})->exportCallback(function ($dob, $signupDate), {
return $age = $signupDate->diffInYears($dob);
}),
...
}
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Arm092\LivewireDatatables\BooleanColumn;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class CallbackDemoTable extends LivewireDatatable
{
public array $defaultFilters = [
'deleted_at' => '0',
];
public function builder(): Builder
{
return User::query()->withTrashed();
}
public function getColumns(): array|Model
{
return [
Column::name('id'),
BooleanColumn::name('deleted_at')->filterable(),
];
}
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class CallbackDemoTable extends LivewireDatatable
{
public string|null|Model $model = User::class
public function getColumns(): array|Model
{
return [
Column::name('users.id'),
Column::name('users.dob')->view('tables.dateview'),
Column::name('users.signup_date')->format(),
];
}
use Arm092\LivewireDatatables\Column;
use Illuminate\Database\Eloquent\Model;
use Arm092\LivewireDatatables\Livewire\LivewireDatatable;
class EditableTable extends LivewireDatatable
{
public string|null|Model $model = User::class;
public function getColumns(): array|Model
{
return [
Column::name('id')
->label('ID')
->linkTo('job', 6),
Column::name('email')
->editable(),
...
];
}
}
public function getPersistKeyProperty()
{
return Auth::id() . '-' . parent::getPersistKeyProperty();
}
/* Migration */
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('complex_queries', static function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id');
$table->string('table');
$table->json('rules');
$table->string('name');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('complex_queries');
}
};
/* Model */
class ComplexQuery extends Model
{
protected $casts = ['rules' => 'array'];
public function user()
{
return $this->belongsTo(User::class);
}
}
/* Datatable Class */
class TableWithSaving extends LivewireDatatable
{
...
public function saveQuery($name, $rules): void
{
Auth::user()->complex_queries()->create([
'table' => $this->name,
'name' => $name,
'rules' => $rules
]);
$this->dispatch('updateSavedQueries', $this->getSavedQueries());
}
public function deleteQuery($id): void
{
ComplexQuery::destroy($id);
$this->dispatch('updateSavedQueries', $this->getSavedQueries());
}
public function getSavedQueries(): void
{
return Auth::user()->complex_queries()->where('table', $this->name)->get();
}
...
}
Column::callback(['name', 'allegiance'], function ($name, $allegiance) { return "$name is allied to $allegiance"; })->filterable(['Rebellion', 'Empire'])->filterOn('users.allegiance')
$value
$row
public function saveQuery(string $name, array $rules)
public function deleteQuery(int $id)
$row
$row
livewire-datatables.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.