Workflow
Beforesearch Hook

This example demonstrates the use of beforeSearch() hook.

Search for "Available" or "Unvailable".

ID
Name
Stock
Price
Created At
1
Arkansas Possum Pie
Available
188,03 €
03/11/2024
2
Albacore Tuna Melt
Unavailable
145,93 €
01/12/2024
3
борщ
Unavailable
100,19 €
02/12/2024
4
Bacalhau com natas
Unavailable
115,28 €
22/11/2024
5
Baba Ghanoush
Unavailable
127,86 €
02/11/2024
6
Bacon Cheeseburger
Available
220,97 €
04/11/2024
7
Baked potato
Available
195,18 €
28/10/2024
8
Baklava
Available
254,15 €
06/12/2024
9
Bangers and mash
Available
172,21 €
11/12/2024
10
Black Pudding
Available
230,45 €
18/12/2024
Showing 1 to 10 of 192 Results
Disclaimer: Table data is randomly generated for illustrative purposes only. The information here is not a reflection of the actual market and does not constitute business, financial, or medical advice.
<?php

namespace App\Livewire\Examples\BeforesearchHookTable;

use App\Models\Dish;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Number;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;

class BeforesearchHookTable extends PowerGridComponent
{
    public string $tableName = 'before-search-table';

    public function setUp(): array
    {
        return [
            PowerGrid::header()
                ->showSearchInput(),

            PowerGrid::footer()
                ->showPerPage()
                ->showRecordCount(),
        ];
    }

    public function datasource(): ?Builder
    {
        return Dish::query();
    }

    public function fields(): PowerGridFields
    {
        return PowerGrid::fields()
            ->add('id')
            ->add('name')
            ->add('price')
            ->add('created_at')
            ->add('in_stock')
            ->add('in_stock_formatted', fn ($dish) => $dish->in_stock ? 'Available' : 'Unavailable')
            ->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR', locale: 'pt_PT'))
            ->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y'));
    }

    public function columns(): array
    {
        return [
            Column::make('ID', 'id')
                ->searchable(),

            Column::make('Name', 'name')
                ->searchable(),

            Column::make('Stock', 'in_stock_formatted', 'in_stock')
                ->searchable(),

            Column::make('Price', 'price_in_eur', 'price')
                ->searchable(),

            Column::make('Created At', 'created_at_formatted', 'created_at'),

        ];
    }

    public function beforeSearch(?string $field = null, ?string $search = null): ?string
    {
        if ($field === 'in_stock') {
            return match (strtolower(trim($search))) {
                'unavailable' => '0',
                'available'   => '1',
                default       => $search,
            };
        }

        if ($field === 'price' && preg_match('/(\d{1,3}(\ \d{3})*|(\d+))(\,\d{2})/', $search)) {
            $parsedCurrency = (new \NumberFormatter('pt-PT', \NumberFormatter::CURRENCY))
                ->parse(preg_replace('/\s+/', "\u{A0}", $search));

            return $parsedCurrency === false ? floatval($search) : $parsedCurrency;
        }

        return $search;
    }
}