PHP code example of givanov95 / laravel-data-table

1. Go to this page and download the library: Download givanov95/laravel-data-table 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/ */

    

givanov95 / laravel-data-table example snippets


return [
    'translatable_relation'      => 'translations',
    'translatable_column'        => 'key',
    'translatable_locale_column' => 'locale',
    'translatable_text_column'   => 'text',

    'global_filter' => 'filter.global',
    'per_page'      => 'perPage',
    'trashed'       => 'filter.trashed',
    'ordering'      => 'ordering',
    'time_zone'     => null,   // null: next to global_filter (filter.timeZone)
    'column_filters' => 'filter', // filter[status]=paid

    'default_per_page' => 15,
    'max_per_page'     => 100,
];

use Givanov95\DataTable\DataTable;
use App\Models\User;

public function index()
{
    $table = (new DataTable(User::query()))
        ->setColumn('id', '#', searchable: true, orderable: true)
        ->setColumn('name', __('Name'), searchable: true, orderable: true)
        ->setColumn('email', __('Email'), searchable: true, orderable: true)
        ->setColumn('action', __('Action'))
        ->process();

    return Inertia::render('Users/Index', [
        'dataTable' => fn () => $table,
    ]);
}

use Givanov95\DataTable\Columns\Column;

$table
    ->setColumn(new Column('id', '#', searchable: true, orderable: true))
    ->setColumn('action', __('Action'));

use App\Models\User;
use Givanov95\DataTable\QueryBuilderTable;

$payload = QueryBuilderTable::for(User::query()->with('roles:id,name'), request())
    ->columns([
        ['key' => 'id', 'label' => __('ID'), 'sortable' => true],
        ['key' => 'first_name', 'label' => __('First name'), 'sortable' => true, 'searchable' => true],
        ['key' => 'email', 'label' => __('Email'), 'sortable' => true, 'searchable' => true],
    ])
    ->allowTrashed()
    ->defaultSort('id', 'desc')
    ->transform(fn (User $user) => [
        'id'         => $user->id,
        'first_name' => $user->first_name,
        'email'      => $user->email,
        'roles'      => $user->roles->map(fn ($r) => ['id' => $r->id, 'name' => $r->name])->all(),
        'deleted_at' => $user->deleted_at?->toDateTimeString(),
    ])
    ->toArray();

->columns([
    ['key' => 'status', 'label' => __('Status'), 'filter' => 'select', 'options' => ['active' => __('Active'), 'blocked' => __('Blocked')]],
    ['key' => 'created_at', 'label' => __('Created'), 'filter' => 'date'],
])

use Givanov95\DataTable\Columns\RelationColumn;

$table->setRelationColumn(
    new RelationColumn('user.name', __('User'), searchable: true, orderable: true)
);

use Givanov95\DataTable\Columns\TranslatableColumn;

$table->setTranslatableColumn(
    new TranslatableColumn(
        locale: app()->getLocale(),
        translationKey: 'title',
        label: __('Title'),
        searchable: true,
        orderable: true,
    )
);

new TranslatableColumn(Locale::Bg, 'title', __('Title'), searchable: true, orderable: true,
    fallbackLocales: [Locale::En, Locale::De]);

// Enum filtering by case name
$table->setEnumColumn('status', App\Enums\OrderStatus::class);

// Numeric-only filtering (currency / numeric input)
$table->setPriceColumn('price');

// Date filtering with timezone-aware parsing
$table->setDateColumn('created_at', 'd.m.Y H:i:s');

$table->setRelation('translations');
$table->setRelation('user', ['id', 'name']);

$table->process(null, function ($query) {
    $query->where('owner_id', auth()->id());
});

// Or for free-form mutations:
$table->advancedSearch(fn ($q) => $q->whereJsonContains('tags', 'featured'));

$table
    // A select of the enum's cases: by value, labelled by the case's label() when it has one.
    ->setColumn('status', __('Status'), filterable: true)
    ->setEnumColumn('status', BookingStatus::class)

    // A select of the app's options: a list of ['value' => …, 'label' => …], a
    // value => label map, or a closure returning either (run once).
    ->setColumn('staff_id', __('Staff'), filterable: true)
    ->setFilterOptions('staff_id', fn () => $business->staff()->pluck('name', 'id')->all())

    // A range of days: filter[starts_at][from]=2026-10-01&filter[starts_at][to]=2026-10-31.
    ->setColumn('starts_at', __('Starts'), orderable: true, filterable: true)
    ->setDateColumn('starts_at', 'd.m.Y H:i')

    // Anything else is a text: contains, or equals with exactMatch.
    ->setRelationColumn(new RelationColumn('customer.name', __('Customer'), filterable: true));

$table->allowTrashed();                          // anyone who sees the list
$table->allowTrashed(ability: 'viewArchive');    // Gate::allows('viewArchive', Post::class)

// routes/web.php
Route::post('posts/{id}/restore', [PostController::class, 'restore'])->name('posts.restore');

// PostController
public function restore(int $id)
{
    return $this->posts->table()->restore($id, fn ($q) => $q->where('team_id', auth()->user()->team_id));
}

$staff = $table->restoreRecord($id, before: function (Staff $staff) use ($business) {
    if (! $business->hasFreeStaffSeat()) {
        throw ValidationException::withMessages(['staff' => __('No free seat on the plan.')]);
    }
});

return back()->with('success', __('Staff member restored.'));
bash
php artisan vendor:publish --tag=data-table-config