PHP code example of reyptr27 / laravel-inertia-table
1. Go to this page and download the library: Download reyptr27/laravel-inertia-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/ */
reyptr27 / laravel-inertia-table example snippets
use ReyPtr27\LaravelQueryBuilderInertiaJs\InertiaTable;
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->searchInput('name');
$table->searchInput(
key: 'framework',
label: 'Find your framework',
defaultValue: 'Laravel'
);
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->selectFilter('language_code', [
'en' => 'Engels',
'nl' => 'Nederlands',
]);
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->selectFilter(
key: 'language_code',
options: $languages,
label: 'Language',
defaultValue: 'nl',
noFilterOption: true,
noFilterOptionLabel: 'All languages'
);
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->toggleFilter('is_verified');
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->toggleFilter(
key: 'is_verified',
label: 'Is email verified',
defaultValue: true,
);
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->numberRangeFilter('invoice_recall_count', 5);
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->toggleFilter(
key: 'invoice_recall_count',
max: 5,
min: 0,
prefix: '',
suffix: '',
step: 1,
label: 'Invoice recall count',
defaultValue: [1,4],
);
});
$users = QueryBuilder::for(/*...*/)
->allowedFilters([NumberRangeFilter::getQueryBuilderFilter('invoice_recall_count')]);
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->customFilter('date_range');
});
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->toggleFilter(
key: 'date_range',
label: 'Date range',
params: ['min_date' => '2022-01-01', 'max_date' => '2022-12-31']
defaultValue: ['start' => '2022-01-01', 'end' => '2022-12-31'],
);
});
$dateRangeFilter = AllowedFilter::custom('date_range', new DateRangeFilter());
$users = QueryBuilder::for(/*...*/)
->allowedFilters([$dateRangeFilter]);
Inertia::render('Page/Index')->table(function (InertiaTable $table) {
$table->withGlobalSearch();
$table->withGlobalSearch('Search through the data...');
});
InertiaTable::defaultGlobalSearch();
InertiaTable::defaultGlobalSearch('Default custom placeholder');
InertiaTable::defaultGlobalSearch(false); // disable
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Support\Collection;
use Inertia\Inertia;
use ReyPtr27\LaravelQueryBuilderInertiaJs\InertiaTable;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
class UserIndexController
{
public function __invoke()
{
$globalSearch = AllowedFilter::callback('global', function ($query, $value) {
$query->where(function ($query) use ($value) {
Collection::wrap($value)->each(function ($value) use ($query) {
$query
->orWhere('name', 'LIKE', "%{$value}%")
->orWhere('email', 'LIKE', "%{$value}%");
});
});
});
$users = QueryBuilder::for(User::class)
->defaultSort('name')
->allowedSorts(['name', 'email', 'language_code'])
->allowedFilters(['name', 'email', 'language_code', $globalSearch])
->paginate()
->withQueryString();
return Inertia::render('Users/Index', [
'users' => $users,
])->table(function (InertiaTable $table) {
$table
->withGlobalSearch()
->defaultSort('name')
->column(key: 'name', searchable: true, sortable: true, canBeHidden: false)
->column(key: 'email', searchable: true, sortable: true)
->column(key: 'language_code', label: 'Language')
->column(label: 'Actions')
->selectFilter(key: 'language_code', label: 'Language', options: [
'en' => 'English',
'nl' => 'Dutch',
]);
}
}
InertiaTable::updateQueryBuilderParameters('companies');
$companies = QueryBuilder::for(Company::query())
->defaultSort('name')
->allowedSorts(['name', 'email'])
->allowedFilters(['name', 'email'])
->paginate(pageName: 'companiesPage')
->withQueryString();
InertiaTable::updateQueryBuilderParameters('users');
$users = QueryBuilder::for(User::query())
->defaultSort('name')
->allowedSorts(['name', 'email'])
->allowedFilters(['name', 'email'])
->paginate(pageName: 'usersPage')
->withQueryString();
return Inertia::render('TwoTables', [
'companies' => $companies,
'users' => $users,
])->table(function (InertiaTable $inertiaTable) {
$inertiaTable
->name('users')
->pageName('usersPage')
->defaultSort('name')
->column(key: 'name', searchable: true)
->column(key: 'email', searchable: true);
})->table(function (InertiaTable $inertiaTable) {
$inertiaTable
->name('companies')
->pageName('companiesPage')
->defaultSort('name')
->column(key: 'name', searchable: true)
->column(key: 'address', searchable: true);
});
vue
<template>
<Table color="red_style" />
</template>
bash
cd app
cp .env.example .env
composer install
npm install
npm run production
touch database/database.sqlite
php artisan migrate:fresh --seed
php artisan dusk:chrome-driver
php artisan serve
php artisan dusk