1. Go to this page and download the library: Download dongrim/datatable-inertia 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/ */
dongrim / datatable-inertia example snippets
'basePath' => '\App\Datatables'
public function index(PostDatatable $datatable)
{
return Inertia::render('Post/Index')->table($datatable);
}
public function index()
{
return Inertia::render('Post/Index')->table(PostDatatable::class); // '\App\Datatables\PostDatatable'
}
public function index(PostDatatable $datatable)
{
return Inertia::render('Post/Index', ['data' => 'some data'])->table($datatable);
}
namespace \App\Datatables;
use Illuminate\Database\Eloquent\Builder;
use Dongrim\DatatableInertia\DatatableInertia;
class PostDatatable extends DatatableInertia
{
/**
* Eloquent datatable query builder
*
* @return Builder
*/
public function query(): Builder
{
// code
}
}
namespace App\Datatables;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Builder;
use Dongrim\DatatableInertia\DatatableInertia;
class PostDatatable extends DatatableInertia
{
public $datatableName = 'post_datatable';
public $perPageKey = 'post_per_page';
public $itemsPerPage = 25;
public $serverSide = true;
public function query(): Builder
{
return Post::select('posts.*', 'users.username as author_name')
->join('users', function ($join) {
$join->on('users.id', '=', 'posts.author_id');
})
->when(request()->search, function ($query, $search) {
return $query->where('title', 'like', "{$search}%")
->orWhere('text', 'like', "{$search}%")
->orWhere('users.username', 'like', "{$search}%");
})
->when(request()->sort, function ($query, $sort) {
$query->withoutGlobalScopes();
$table = ($sort == 'id') ? "posts." : "";
$query->orderBy($table . $sort, request()->order ?? 'asc');
})
->when(request()->active, function ($query, $active) {
$active = $active === 'true' ? true : false;
$query->where('posts.active', $active);
});
}
public function columns(): array
{
return ['id', 'position', 'active', 'title', 'text', 'author_name'];
}
public function filters(): array
{
return ['sort', 'order', 'search', 'active'];
}
public function guard($data): array
{
return [
'edit' => Auth::user()->can('post.edit') ? route('post.edit', $data->id) : null,
'destroy' => Auth::user()->can('post.destroy') ? route('post.destroy', $data->id) : null,
'restore' => Auth::user()->can('post.restore') ? route('post.restore', $data->id) : null,
];
}
public function modify($data)
{
$data->text = str($data->text)->substr(0, 100);
return $data;
}
}