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

Video Version of the Lesson

[Only for premium members]

Link to the repository

[Only for premium members]

Text Version of the Lesson

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:

php artisan make:model Order -m

Migration:

Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('product_id')->constrained();
$table->integer('price');
$table->timestamps();
});

app/Models/Order.php:

class Order extends Model
{
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 added 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...

The full lesson is only for Premium Members.
Want to access all 24 video+text lessons of this course? (2 h 01 min)

You also get:

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