Skip to content

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".

php
// 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.

php
// 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.

php
// 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.

php
// 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.

php
// 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.

Query String โ€‹

To enable the Query functionality, you must declare a method queryString() inside your Table Component class.

php
// 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:

php
// app/Livewire/DishTable.php

use PowerComponents\LivewirePowerGrid\PowerGridComponent;

class DishTable extends PowerGridComponent
{
    protected function queryString(): array
    {
        return [
            'search' => ['except' => ''],
            'page' => ['except' => 1],
            ...$this->powerGridQueryString(),
        ];
    }
}

Created By Luan Freitas and @DanSysAnalyst