Workflow
Action Rules

This example illustrates the use of Action Rules to alter rows matching predefined conditions.
The component below has a rule to replace the "edit button" when the "ID is equal 5".

ID
Name
Category
Price
In Stock
Created At
Action
1
Arkansas Possum Pie
Pie
$95.00
Yes
05/04/2024
2
Albacore Tuna Melt
Dessert
$208.00
Yes
05/04/2024
3
борщ
Fish
$277.00
Yes
05/04/2024
4
Bacalhau com natas
Garnish
$149.00
Yes
05/04/2024
5
Baba Ghanoush
Pasta
$56.00
No
05/04/2024
6
Bacon Cheeseburger
Pie
$198.00
No
05/04/2024
7
Baked potato
Fish
$206.00
Yes
05/04/2024
8
Baklava
Dessert
$67.00
No
05/04/2024
9
Bangers and mash
Meat
$79.00
Yes
05/04/2024
10
Black Pudding
Dessert
$108.00
Yes
05/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.
open in GitHub
<?php
 
use App\Models\Dish;
use Illuminate\Support\Carbon;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Rule;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\Header;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
class ActionRulesTable extends PowerGridComponent
{
public string $tableName = 'actionRulesTable';
 
public function setUp(): array
{
return [
Header::make()
->showSearchInput(),
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource()
{
return Dish::with('category');
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('category_id', function ($dish) {
return $dish->category_id;
})
->add('category_name', function (Dish $dish) {
return $dish->category->name;
})
->add('price_fmt', function (Dish $dish) {
return (new \NumberFormatter('en_US', \NumberFormatter::CURRENCY))
->formatCurrency($dish->price, 'USD');
})
->add('in_stock', function (Dish $dish) {
return $dish->in_stock ? 'Yes' : 'No';
})
->add('created_at_formatted', function (Dish $dish) {
return Carbon::parse($dish->created_at)
->timezone('America/Sao_Paulo')->format('d/m/Y');
});
}
 
public function beforeSearch(?string $field = null, ?string $search = null): ?string
{
if ($field === 'in_stock') {
return str(strtolower($search))
->replace('no', '0')
->replace('yes', '1')
->toString();
}
 
return $search;
}
 
public function columns(): array
{
return [
 
Column::make('ID', 'id')
->searchable()
->sortable(),
 
Column::make('Name', 'name')
->bodyAttribute('!text-wrap') // <--- add "!"
->searchable()
->sortable(),
 
Column::make('Category', 'category_name'),
 
Column::make('Price', 'price_fmt', 'price')
->searchable()
->sortable(),
 
Column::make('In Stock', 'in_stock')
->searchable(),
 
Column::make('Created At', 'created_at_formatted'),
 
Column::action('Action'),
];
}
 
#[\Livewire\Attributes\On('edit')]
public function edit(int $dishId): void
{
$this->js('alert('.$dishId.')');
}
 
public function actions(Dish $row): array
{
return [
Button::add('edit')
->slot('Edit: '.$row->id)
->class('text-center'),
];
}
 
public function actionRules($row): array
{
return [
Rule::button('edit')
->when(fn (Dish $dish) => $dish->id == 5)
->bladeComponent('livewire-powergrid::icons.arrow', [
'dish-id' => 'id',
'class' => 'w-5 h-5 !text-red-500',
]),
];
}
}

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

Name
Version
Description
laravel/framework
v10
The Laravel Framework.
openspout/openspout
v4.23.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
5.x-dev
PowerGrid generates Advanced Datatables using Laravel Livewire.