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
Parameter | Description | Default |
---|---|---|
(string) $label | The argument $label sets the button caption. | 'Sum' |
(bool) $header | If is true , Powergrid will create a row in the table below the filters. | false |
(bool) $string | If 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:
withCount
- Will display the count of all records in the field
Parameter | Description | Default |
---|---|---|
(string) $label | The argument $label sets the button caption. | 'Count' |
(bool) $header | If is true , Powergrid will create a row in the table below the filters. | false |
(bool) $string | If is true , Powergrid will create a row in the footer of the table. | false |
Example:
//...
Column::make(__('Price'), 'price')
->withCount('Count', true, false),
Result:
withAvg
- Will display the avg of all records in the field
Parameter | Description | Default |
---|---|---|
(string) $label | The argument $label sets the button caption. | 'Avg' |
(bool) $header | If is true , Powergrid will create a row in the table below the filters. | false |
(bool) $string | If is true , Powergrid will create a row in the footer of the table. | false |
Example:
//...
Column::make(__('Price'), 'price')
->withAvg('Avg', true, false),
Result:
withMin
- Will display the min of all records in the field
Parameter | Description | Default |
---|---|---|
(string) $label | The argument $label sets the button caption. | 'Min' |
(bool) $header | If is true , Powergrid will create a row in the table below the filters. | false |
(bool) $string | If is true , Powergrid will create a row in the footer of the table. | false |
Example:
//...
Column::make(__('Price'), 'price')
->withMin('Min', true, false),
Result:
withMax
- Will display the max of all records in the field
Parameter | Description | Default |
---|---|---|
(string) $label | The argument $label sets the button caption. | 'Max' |
(bool) $header | If is true , Powergrid will create a row in the table below the filters. | false |
(bool) $string | If is true , Powergrid will create a row in the footer of the table. | false |
Example:
//...
Column::make(__('Price'), 'price')
->withMax('Max', true, false),
Result:
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),
];
}