Column Summary

PowerGrid can include data summaries inside each columns() header.

Summaries can display the column's sum, count, average, min and max value.

Usage

Summaries are chained to the Column::add() method.

withSum

  • Will display the sum of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Sum'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $stringIf is true, Powergrid will create a row in the footer of the table.false

WARNING

This will pre-process all the data in your database to work with the sum of all records. ->get(), in all cases, only one request is made;

Example:

//...
Column::make(__('Price'), 'price')
    ->withSum('Sum', true, false),



Result: Output


withCount

  • Will display the count of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Count'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $stringIf is true, Powergrid will create a row in the footer of the table.false

Example:

//...
Column::make(__('Price'), 'price')
    ->withCount('Count', true, false),



Result: Output


withAvg

  • Will display the avg of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Avg'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $stringIf is true, Powergrid will create a row in the footer of the table.false

Example:

//...
Column::make(__('Price'), 'price')
    ->withAvg('Avg', true, false),



Result: Output


withMin

  • Will display the min of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Min'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $stringIf is true, Powergrid will create a row in the footer of the table.false

Example:

//...
Column::make(__('Price'), 'price')
    ->withMin('Min', true, false),



Result: Output


withMax

  • Will display the max of all records in the field
ParameterDescriptionDefault
(string) $labelThe argument $label sets the button caption.'Max'
(bool) $headerIf is true, Powergrid will create a row in the table below the filters.false
(bool) $stringIf is true, Powergrid will create a row in the footer of the table.false

Example:

//...
Column::make(__('Price'), 'price')
    ->withMax('Max', true, false),



Result: Output

Summarizing formatted data

PowerGrid provides a convenient way to use closures to display formatted data in your table.

To summarize formatted data (e.g, currency), you must pass the formatted column and original column to the methdo field():

In the example next example, we have a column price_BRL formatting the amount in Brazilian Real currency format.

  public function addColumns(): PowerGridEloquent
    {
        return PowerGrid::eloquent()
            ->addColumn('id')
            
            //1000.00
            ->addColumn('price')
            
            //R$ 1.000,00
            ->addColumn('price_BRL', fn (Dish $dish) => 'R$ ' . number_format(e($dish->price), 2, ',', '.'));
    }

Next, we must pass the two columns to the field() method, when including the "formatted price" column in our table:

-Column price_BRL containing the formatted value (R$ 1.000,00).

-Column price containing the raw amount (1000.00);

public function columns(): array
{
    return [
        Column::make(__('ID'), 'id', 'dishes.id')
            ->searchable()
            ->sortable(),
            
        Column::make(__('Price'), 'price_BRL', 'price') //formatted field, original field
            ->withSum('Total amount', true, true),
    ];
}