Filament also offers convenient ways to group table data by columns and calculate the aggregated data like "sum" or "average" to show at the bottom.
To demonstrate that, I've created a new Model for Orders:
app/Models/Order.php:
class Order extends Model{ use HasFactory; protected $fillable = ['user_id', 'product_id', 'price']; public function user(): BelongsTo { return $this->belongsTo(User::class); } public function product(): BelongsTo { return $this->belongsTo(Product::class); }}
And seeded some data for the orders:
Then, we can generate a new Filament Resource to show the table of Orders:
php artisan make:filament-resource Order
The table has these values:
app/Filament/Resources/OrderResource.php:
class OrderResource extends Resource{ // ... public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('created_at') ->dateTime(), Tables\Columns\TextColumn::make('product.name'), Tables\Columns\TextColumn::make('user.name'), Tables\Columns\TextColumn::make('price') ->money('usd') ->getStateUsing(function (Order $record): float { return $record->price / 100; }) ]) ->defaultSort('created_at', 'desc') // ... other methods with default values }}
Here's how it looks now:
Grouping by Product Name
What if you want to divide this table by some condition? Let's try to group by product. Then, you just add...