Searching Data โ
This section covers PowerGrid's Search functionality.
Here you will find:
Introduction โ
The methods in this section apply to Table Header Search Input and/or the individual Column Filters.
Before Search Hook โ
Sometimes, you may need to reverse data formatting before executing a Search Input database query. This is often the case with currency, boolean phone numbers, and other field types.
With PowerGrid you can use the method beforeSearch() to intercept a search string, and prepare it to be searched in the database.
The next example demonstrates cleaning a phone number before searching. Users might search for a "+1-999-123-1235", while the database record is "19991231235".
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
public function beforeSearch(string $field = null, string $search = null)
{
if ($field === 'phone') {
//+1-999-123-1235 => 19991231235
return str($search)->replaceMatches('/[^0-9]+/', '')->toString();
}
return $search;
}
}You may also create a dedicated method for each field you want to intercept.
The method name must be: beforeSearch+ fieldname in camel case. For example, beforeSearchPhone() for the field phone.
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
public function beforeSearchPhone($search): string
{
return str($search)->replaceMatches('/[^0-9]+/', '')->toString();
}
}๐ See it in action
See an interactive example using Before Search Hook.
Searching Custom Fields โ
Here you will find examples using the beforeSearch() method to reverse Custom Field Data Formatting, preparing data for database searches.
Currency โ
The example below reverses the Currency data formatting.
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
public function beforeSearch(string $field = null, string $search = null)
{
if ($field === 'price') {
$parsedCurrency = (new \NumberFormatter('pt-PT', \NumberFormatter::CURRENCY))
->parse(preg_replace('/\s+/', "\u{A0}", $search));
return $parsedCurrency == false ? floatval($search) : $parsedCurrency;
}
return $search;
}
}Boolean โ
The example below reverses the Boolean data formatting.
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
public function beforeSearch(string $field = null, string $search = null)
{
if ($field === 'in_stock') {
return match (strtolower(trim($search))) {
'yes' => '1',
'no' => '0',
default => $search,
};
}
return $search;
}
}Date โ
Consider using the searchableRaw() column method.
Enum Field โ
Currently, PowerGrid does not support searching Enum fields with the Search Input. Consider using a Filter Enum Select instead.
Searching with Relationship โ
To include relationships when searching with Search Input or Column Filters, you must indicate these relationships in the relationSearch() method in your Table Component.
The method returns an associative array with model_name => columns to be searched. Nested relationships should also be indicated in this array.
The next example adds the relationship to the kitchen Model and includes the column name and the nested relationship with the Chef Model. A Dish has a Kitchen, and a Kitchen has a Chef.
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
public function relationSearch(): array
{
return [
'kitchen' => [ // relationship on dishes model
'name', // column enabled to search
'chef' => ['name'] // nested relation and column enabled to search
],
];
}
}๐ See it in action
See an interactive example using Relation Search.
Custom Search Handler โ
The global search logic is resolved via the Laravel container, allowing you to replace the default implementation with your own handler.
SearchHandlerContract โ
Your custom handler must implement the SearchHandlerContract interface:
namespace PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
interface SearchHandlerContract
{
public function apply(EloquentBuilder|QueryBuilder $query): EloquentBuilder|QueryBuilder;
}Binding Your Handler โ
Register your custom handler in a Service Provider (e.g., AppServiceProvider::register()):
use PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers\SearchHandlerContract;
use App\Support\Powergrid\Handlers\SearchHandler;
public function register(): void
{
$this->app->bind(SearchHandlerContract::class, function ($app, array $params) {
return new SearchHandler($params['component']);
});
}Example: Custom Handler โ
// app/Support/PowerGrid/Handlers/SearchHandler.php
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use PowerComponents\LivewirePowerGrid\DataSource\Processors\Database\Handlers\SearchHandlerContract;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class SearchHandler implements SearchHandlerContract
{
public function __construct(
protected readonly PowerGridComponent $component
) {}
public function apply(EloquentBuilder|QueryBuilder $query): EloquentBuilder|QueryBuilder
{
$search = $this->component->search;
if (empty($search)) {
return $query;
}
return $query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
});
}
}TIP
The default SearchHandler uses protected methods, so you can also extend it instead of replacing it entirely.
Query String โ
To enable the Query functionality, you must declare a method queryString() inside your Table Component class.
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
protected function queryString(): array
{
return $this->powerGridQueryString();
}
}You can also exclude some fields from the query string, as demonstrated below. For more information, visit Livewire excluding query string documentation section.
Example:
// app/Livewire/DishTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
class DishTable extends PowerGridComponent
{
protected function queryString(): array
{
return [
'search' => ['except' => ''],
'page' => ['except' => 1],
...$this->powerGridQueryString(),
];
}
}