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
78,43 €
09/05/2024
2
Albacore Tuna Melt
Available
89,90 €
29/04/2024
3
борщ
Available
100,19 €
24/04/2024
4
Bacalhau com natas
Available
213,37 €
10/05/2024
5
Baba Ghanoush
Unavailable
263,16 €
18/03/2024
6
Bacon Cheeseburger
Unavailable
61,67 €
09/04/2024
7
Baked potato
Unavailable
148,63 €
26/03/2024
8
Baklava
Unavailable
127,07 €
12/04/2024
9
Bangers and mash
Available
181,73 €
04/05/2024
10
Black Pudding
Available
133,18 €
01/04/2024
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\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class BeforesearchHookTable extends PowerGridComponent
{
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return Dish::query();
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->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') {
$search = match (strtolower(trim($search))) {
'available' => '1',
'unavailable' => '0',
default => $search,
};
}
 
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;
}
}
Code highlighting provided by Torchlight.dev

Here you can find all relevant packages installed on this demo.

Name
Version
Description
laravel/framework
v11
The Laravel Framework.
livewire/livewire
v3.4.12
A front-end framework for Laravel.
openspout/openspout
v4.24.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
v5.6.3
PowerGrid generates Advanced Datatables using Laravel Livewire.