Skip to content

Summarizing Data โ€‹

Introduction โ€‹

PowerGrid can show aggregate values for a column in the table header, footer, or both. Built-in operations are sum, count, avg, min, and max. Custom closures use withSummary().

Database datasources compute every requested built-in aggregate in a single query. Collection datasources derive the same operations in memory.

Built-in operations โ€‹

Chain summarize() on a Column::make() or Column::add() column. You can chain more than one operation on the same column.

phpapp/Livewire/ProductTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Column;

class ProductTable extends PowerGridComponent
{
    public function columns(): array
    {
        return [
            Column::make('Price', 'price')
                ->summarize('sum', 'Sum Price', header: true, footer: false)
                ->summarize('avg', 'Avg Price', header: true, footer: false)
                ->summarize('min', 'Min Price', header: true, footer: false)
                ->summarize('max', 'Max Price', header: true, footer: false)
                ->summarize('count', 'Count Price', header: true, footer: false),
        ];
    }
}
ParameterDescriptionDefault
(string) $operationOne of sum, count, avg, min, max.required
(string) $labelCaption shown next to the value.required
(bool) $headerIf true, the result is shown in the table header, under the filters.false
(bool) $footerIf true, the result is shown in the table footer.true

An unknown $operation throws InvalidArgumentException.

Examples per operation:

php
Column::make('Price', 'price')->summarize('sum', 'Sum Price', header: true, footer: false);
Column::make('Price', 'price')->summarize('count', 'Count Price', header: true, footer: false);
Column::make('Price', 'price')->summarize('avg', 'Avg Price', header: true, footer: false);
Column::make('Price', 'price')->summarize('min', 'Min Price', header: true, footer: false);
Column::make('Price', 'price')->summarize('max', 'Max Price', header: true, footer: false);

Custom summaries โ€‹

Use withSummary() when a built-in aggregate is not enough. The closure receives the filtered query (cloned) or, for a Collection datasource, a cloned collection.

phpapp/Livewire/ProductTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Column;

class ProductTable extends PowerGridComponent
{
    public function columns(): array
    {
        return [
            Column::make('Price', 'price')
                ->withSummary(
                    'expensive',
                    'Expensive',
                    fn ($query) => $query->where('price', '>=', 300)->count(),
                    header: true,
                    footer: true,
                ),
        ];
    }
}
ParameterDescriptionDefault
(string) $keyUnique key for this custom summary.required
(string) $labelCaption shown next to the value.required
(Closure) $usingCallback. Query datasource: cloned builder. Collection: cloned collection.required
(bool) $headerIf true, the result is shown in the table header, under the filters.false
(bool) $footerIf true, the result is shown in the table footer.true

The closure is kept server-side. It is not serialized with the column payload.

Formatting summarized data โ€‹

Override summarizeFormat() and return an array of field.{operations} keys mapped to a formatter closure. List operations comma-separated inside the braces.

Operations are: sum, avg, count, min, max.

phpapp/Livewire/ProductTable.php
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use Illuminate\Support\Number;

class ProductTable extends PowerGridComponent
{
    public function summarizeFormat(): array
    {
        return [
            'price.{sum,avg,min,max}' => fn ($value) => Number::currency($value, in: 'USD'),
            'price.{count}' => fn ($value) => Number::format($value, locale: 'br'),
            'calories.{avg}' => fn ($value) => Number::format($value, locale: 'br', precision: 2).' kcal',
        ];
    }
}

Created By Luan Freitas and @DanSysAnalyst