Search
Global Search
In addition to the extensive filtering options, you can enable a global search field that will search across one or more columns with a wildcard search.
Enable by the Column class
One way to enable global search is by passing the searchable
argument to the column's make()
method:
TextColumn::make('name', searchable: true);
Alternatively, you may use the searchable()
and notSearchable()
methods:
TextColumn::make('name')->searchable();
TextColumn::make('name')->notSearchable();
Enable on the Table class
Another way to enable global search is by setting the $searchable
property on your Table class:
class Users extends Table
{
protected array|string $search = ['name', 'email'];
}
Customize the Search Query
You may customize the search query by using the withQueryBuilder()
method. This allows you to interact with the built-in Query Builder before the results are fetched. On the QueryBuilder
instance, you can use the searchUsing()
method to customize the search query. It takes three arguments: the Eloquent Query Builder instance, the requested search term as a string, and the requested search terms parsed into a Collection.
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use InertiaUI\Table\QueryBuilder;
class Users extends Table
{
public function withQueryBuilder(QueryBuilder $queryBuilder)
{
$queryBuilder->searchUsing(function (Builder $query, string $search, Collection $terms) {
$locale = app()->getLocale();
$query->where("name->{$locale}", 'like', "%{$search}%");
});
}
}
Note that you should take care of empty search terms yourself and handle them accordingly.
Autofocus
By default, the global search field is focused when the page loads. If you want to disable this behavior, you can set the $autofocus
property to TableComponent::None
:
use InertiaUI\Table\TableComponent;
class Users extends Table
{
protected ?TableComponent $autofocus = TableComponent::None;
}
If you want this to be the default for all tables, you may call the static defaultAutofocus()
method on the Table class, for example, in your AppServiceProvider
:
use InertiaUI\Table\Table;
use InertiaUI\Table\TableComponent;
Table::defaultAutofocus(TableComponent::None);