Courses

Filament 3 From Scratch: Practical Course

Table Grouping and Summarizers

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Grouping table data
- Adding column summaries
- Implementing sum/average calculations
- Understanding query optimization

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...

The full lesson is only for Premium Members.
Want to access all 24 lessons of this course? (101 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord