Workflow
Summarize

This example uses the Column Summary feature.

ID
Name
Calories
Price
Created At
Average: 567,90 kcal
Sum Price: 29 993,46 €
Count Price: 192 price(s)
Avg Price: 156,22 €
1
Arkansas Possum Pie
434 kcal
118,08 €
26/09/2024
2
Albacore Tuna Melt
828 kcal
132,75 €
07/10/2024
3
борщ
391 kcal
100,19 €
26/09/2024
4
Bacalhau com natas
573 kcal
208,60 €
29/09/2024
5
Baba Ghanoush
735 kcal
183,81 €
03/10/2024
6
Bacon Cheeseburger
806 kcal
137,48 €
11/09/2024
7
Baked potato
534 kcal
189,66 €
03/10/2024
8
Baklava
750 kcal
161,92 €
12/10/2024
9
Bangers and mash
203 kcal
214,04 €
03/10/2024
10
Black Pudding
330 kcal
116,13 €
03/10/2024
1: 51,26 €
Max Price: 276,41 €
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\SummarizeTable;
 
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 SummarizeTable extends PowerGridComponent
{
public string $tableName = 'summarize-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('calories', fn ($dish) => $dish->calories . ' kcal')
->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()
->sortable(),
 
Column::make('Name', 'name')
->searchable()
->sortable(),
 
Column::make('Calories', 'calories', 'calories')
->withAvg('Average', header: true, footer: false)
->sortable(),
 
Column::make('Price', 'price_in_eur', 'price')
->withSum('Sum Price', header: true, footer: false)
->withAvg('Avg Price', header: true, footer: false)
->withCount('Count Price', header: true, footer: false)
->withMin('Min Price', header: false, footer: true)
->withMax('Max Price', header: false, footer: true)
->sortable(),
 
Column::make('Created At', 'created_at_formatted'),
];
}
 
public function summarizeFormat(): array
{
return [
'price.{sum,avg,min,max}' => fn ($value) => Number::currency($value, in: 'EUR', locale: 'pt_PT'),
'price.{count}' => fn ($value) => Number::format($value, locale: 'br') . ' price(s)',
'calories.{avg}' => fn ($value) => Number::format($value, locale: 'br', precision: 2) . ' kcal',
];
}
}
Code highlighting provided by Torchlight.dev