Skip to content

Instantly share code, notes, and snippets.

@sjardim
Created April 19, 2023 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjardim/59a919c7d128d16e80edea0dcf766f02 to your computer and use it in GitHub Desktop.
Save sjardim/59a919c7d128d16e80edea0dcf766f02 to your computer and use it in GitHub Desktop.
Adds a sum, avg, count column to a Laravel Filament table
//Adapted from https://filamentphp.com/tricks/aggregate-data-in-table-footer
// On your Resource list page
protected function getTableContentFooter(): View
{
return view('tables.footer', [
'calc_columns' => [
[
'column' => 'monthly_fee_in_cents',
'operation' => 'sum',
'format'=> 'money',
'currency' => 'USD',
],
],
'hasCheckboxColumn' => false
]);
}
// On the view tables.footer
@php use Filament\Tables\Table; @endphp
<x-tables::row>
@if($hasCheckboxColumn)
<x-tables::cell>
{{-- for the checkbox column --}}
</x-tables::cell>
@endif
@foreach ($columns as $column)
<x-tables::cell
wire:loading.remove.delay
wire:target="{{ implode(',', Table::LOADING_TARGETS) }}"
>
@for ($i = 0; $i < count($calc_columns); $i++ )
@if ($column->getName() == $column = $calc_columns[$i]['column'])
@php
$operation = $calc_columns[$i]['operation'];
$money = $calc_columns[$i]['format'] ?? false;
$currency = $calc_columns[$i]['currency'] ?? 'USD';
@endphp
<div class="filament-tables-column-wrapper">
<div class="filament-tables-text-column px-4 py-2 flex w-full {{$money ? 'justify-end text-end' : 'justify-start text-start'}}">
<div class="inline-flex items-center space-x-1 rtl:space-x-reverse">
<span class="font-bold">
@if($money)
{{ money($records->$operation($column), $currency)->format() }}
@else
{{ $records->$operation($column) }}
@endif
</span>
</div>
</div>
</div>
@endif
@endfor
</x-tables::cell>
@endforeach
</x-tables::row>
@sjardim
Copy link
Author

sjardim commented Apr 19, 2023

Preview:
CleanShot 2023-04-19 at 21 18 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment