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
{
}
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));
}
}
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
{
}