Workflow
Summarize

This example uses the Column Summary feature.

ID
Name
Calories
Price
Created At
Average: 526,77 kcal
Sum Price: 31 495,79 €
Count Price: 192 price(s)
Avg Price: 164,04 €
1
Arkansas Possum Pie
178 kcal
188,03 €
03/11/2024
2
Albacore Tuna Melt
855 kcal
145,93 €
01/12/2024
3
борщ
184 kcal
100,19 €
02/12/2024
4
Bacalhau com natas
444 kcal
115,28 €
22/11/2024
5
Baba Ghanoush
118 kcal
127,86 €
02/11/2024
6
Bacon Cheeseburger
233 kcal
220,97 €
04/11/2024
7
Baked potato
843 kcal
195,18 €
28/10/2024
8
Baklava
731 kcal
254,15 €
06/12/2024
9
Bangers and mash
734 kcal
172,21 €
11/12/2024
10
Black Pudding
360 kcal
230,45 €
18/12/2024
1: 50,77 €
Max Price: 279,36 €
Showing 1 to 10 of 192 Results
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',
        ];
    }
}