PHP code example of starter-solutions / inertia-data-table

1. Go to this page and download the library: Download starter-solutions/inertia-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/ */

    

starter-solutions / inertia-data-table example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use StarterSolutions\InertiaDataTable\Attributes\AllowedSorts;

#[AllowedSorts(['id', 'name', 'email', 'created_at'])]
class User extends Model
{
}

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::get('/users', function () {
    return Inertia::render('Users/Index', [
        'users' => User::query()->dataTable(
            tableKey: 'users',
            filterUsing: function (Builder $query, array $filter): void {
                $search = trim((string) ($filter['search'] ?? ''));

                if ($search !== '') {
                    $query->where(function (Builder $query) use ($search): void {
                        $query
                            ->where('name', 'like', "%{$search}%")
                            ->orWhere('email', 'like', "%{$search}%");
                    });
                }
            },
            defaultPerPage: 25,
            defaultSortBy: 'name',
            defaultDescending: false,
        ),
    ]);
});

#[AllowedSorts(['id', 'name', 'email'])]
class User extends Model
{
}

User::query()->dataTable(
    tableKey: 'users',
    allowedSorts: ['id', 'name'],
);

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use StarterSolutions\InertiaDataTable\Attributes\AllowedSorts;

#[AllowedSorts(['id', 'title', 'author_id'])]
class Post extends Model
{
    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'author_id');
    }
}

#[AllowedSorts(['name', 'email'])]
class User extends Model
{
}

$posts = Post::query()
    ->with('author')
    ->dataTable('posts');

use Illuminate\Database\Eloquent\Casts\Attribute;

#[AllowedSorts([
    'id',
    'display_name' => 'name',
])]
class User extends Model
{
    protected $appends = ['display_name'];

    protected function displayName(): Attribute
    {
        return Attribute::get(fn (): string => strtoupper($this->name));
    }
}

User::query()->dataTable(
    tableKey: 'users',
    allowedSorts: [
        'name',
        'name_length' => fn (Builder $query, string $direction) =>
            $query->orderByRaw("length(name) {$direction}"),
    ],
);

use Illuminate\Database\Eloquent\Builder;
use StarterSolutions\InertiaDataTable\Contracts\SortCallback;

final class NameLengthSort implements SortCallback
{
    public function __invoke(Builder $query, string $direction): void
    {
        $query->orderByRaw("length(name) {$direction}");
    }
}

#[AllowedSorts([
    'name',
    'name_length' => NameLengthSort::class,
])]
class User extends Model
{
}

User::query()->dataTable(
    tableKey: 'users',
    filterUsing: fn (Builder $query, array $filter) => $query
        ->when($filter['status'] ?? null, fn (Builder $query, string $status) =>
            $query->where('status', $status)),
    additional: [
        'filters' => [
            'statuses' => ['active', 'invited', 'disabled'],
        ],
    ],
);

$users = DB::table('users')->dataTable(
    tableKey: 'users',
    allowedSorts: ['id', 'name', 'email'],
);

$users = collect($items)->dataTable(
    tableKey: 'users',
    allowedSorts: ['name', 'profile.city'],
);

return Inertia::render('Dashboard', [
    'users' => User::query()->dataTable('users'),
    'orders' => Order::query()->dataTable('orders'),
]);

'users' => UserResource::collection(
    User::query()->dataTable('users')
),

return [
    'session_routes_middleware' => ['web'],
    'table_key_param' => 'tableKey',
    'per_page_param' => 'per_page',
    'sort_by_param' => 'sort_by',
    'descending_param' => 'descending',
    'page_name_param' => 'page',
    'filter_param' => 'filter',
    'default_per_page' => 15,
    'default_sort_by' => 'id',
    'default_descending' => true,
];