Using tables with aggregated / grouped data

Jul 21, 2022
Simon Bühler
Table builder, Admin panel

When working with tables for reports / statistics often aggregated data is used, e.g. grouped by day.

To customize the query overwrite the getTableQuery in the ListPage, make sure it returns also the unique key

protected function getTableQuery(): Builder
return Participant::select(
DB::raw('MIN(id) as id'),
DB::raw('DATE(datetimecolumn) as date'),
DB::raw('count(*) as total'))
->orderBy('date', 'DESC')

Obviously it makes no sense to offer a edit page for the grouped records, but when you want to show a modal or more details for the grouped row you can either overwrite resolveTableRecord to return a model that should be used by $record:

protected function resolveTableRecord(?string $key): ?Model
return Participant::find($key)->first();

Another way is to pass the key into a Action closure via $livewire->mountedTableActionRecord and gather the data from there:

Action::make('Details')->action(fn ($livewire) => dump($livewire->mountedTableActionRecord))

No comments yet…