PHP code example of yuisalabs / voltia-datatable

1. Go to this page and download the library: Download yuisalabs/voltia-datatable 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/ */

    

yuisalabs / voltia-datatable example snippets




namespace App\Tables;

use Illuminate\Database\Eloquent\Builder;
use App\Models\User;
use Yuisalabs\VoltiaDatatable\Table;
use Yuisalabs\VoltiaDatatable\Column;
use Yuisalabs\VoltiaDatatable\Filters\SelectFilter;
use Yuisalabs\VoltiaDatatable\Filters\DateRangeFilter;

class UserTable extends Table
{
    public function query(): Builder
    {
        return User::query();
    }

    public function columns(): array
    {
        return [
            Column::make('id', 'ID')
                ->sortable(),
            
            Column::make('name', 'Name')
                ->sortable()
                ->searchable(),
            
            Column::make('email', 'Email')
                ->sortable()
                ->searchable(),
            
            Column::make('status', 'Status')
                ->sortable()
                ->format(fn ($row, $value) => ucfirst($value)),
            
            Column::make('created_at', 'Created At')
                ->sortable()
                ->format(fn ($row, $value) => $value?->format('Y-m-d H:i:s')),
        ];
    }

    protected function filters(): array
    {
        return [
            'status' => new SelectFilter('status', [
                'active' => 'Active',
                'inactive' => 'Inactive',
                'pending' => 'Pending',
            ]),
            'created_at' => new DateRangeFilter('created_at'),
        ];
    }
}



namespace App\Http\Controllers;

use App\Tables\UserTable;
use Inertia\Inertia;

class UserController extends Controller
{
    public function index(UserTable $datatable)
    {
        return Inertia::render('Users/Index', [
            'datatable' => $datatable->make(),
        ]);
    }
}

Column::make('key', 'Label')
    ->sortable()          // Enable sorting
    ->searchable()        // Enable searching
    ->hidden()            // Hide column
    ->align('center')     // Alignment: left, center, right
    ->minWidth(150)       // Minimum width in pixels
    ->format(fn ($row, $value) => ...) // Custom formatting

public function columns(): array
{
    return [
        Column::make('user.name', 'User Name')
            ->sortable()
            ->searchable(),
        
        Column::make('user.email', 'Email')
            ->searchable(),
    ];
}

'status' => new SelectFilter('status', [
    'active' => 'Active',
    'inactive' => 'Inactive',
])

'search' => new TextFilter('column_name')

'is_verified' => new BooleanFilter('is_verified')

'created_at' => new DateRangeFilter('created_at')

public function query(): Builder
{
    return User::query()
        ->where('status', 'active')
        ->with('roles');
}

Column::make('price', 'Price')
    ->format(fn ($row, $value) => 'Rp ' . number_format($value, 0, ',', '.'))

Column::make('status', 'Status')
    ->format(fn ($row, $value) => match($value) {
        'active' => '✅ Active',
        'inactive' => '❌ Inactive',
        default => '⏸ Pending'
    })

return [
    'default_per_page' => 15,
    'per_page_options' => [10, 15, 25, 50, 100],
    'max_per_page' => 100,
    'query_string' => true,
    'date_format' => 'Y-m-d',
];
bash
php artisan vendor:publish --tag="voltia-datatable-config"
bash
php artisan make:datatable UserTable --model=User