Skip to main content

Table Grouping and Summarizers

Premium
2:42

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 of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Caspar Bisschop avatar

Very usefull and a nice new feature in filament v3

Ali Al Qahtani avatar

Great 👍

Emile Jobity avatar

I got wrong valuesr with this code where it divides by 100 as my price value is stored as a decimal in the database. I just removed the 100 instead and it works ->summarize(Tables\Columns\Summarizers\Sum::make() ->formatStateUsing(fn ($state) => '$' . number_format($state , 2))),

👍 1
Povilas Korop avatar

Yes, if you store value as a decimal, you don't need to divide by 100. To be honest, I regret putting this integer-money logic in this course as it may confuse some people. But that's how I would build it in my projects.

Jeroen Willemsen avatar

is there a way to defaultSort on number. like 1,2,3,4,5 now i get 1,10,11,12,2

Nerijus avatar

How do you sort and on what kind of field?

Jeroen Willemsen avatar

i already solve my problem. i make the in the db tabke a string instead integer. after i chance that it work just fine

flcYtb avatar

Thank you for the wonderful lesson. I'm having fun building it. Now, regarding this summarize() method, when I run it, the calculation results and display are generally good, but the title of the total becomes as shown below.

filament-tables::table.summary.subheadings.group
filament-tables::table.summary.summarizers.sum.label
$189.98
filament-tables::table.summary.heading
filament-tables::table.summary.summarizers.sum.label
$389.92

It seems like I could use something like label(), but I don't know where to put it. What should I do? (This is a Japanese machine translation.)

Nerijus avatar

I don't exactly understand what you get. Is it instead of text you get filament-tables::table.summary.subheadings.group? If so than I would guess not everything is translated to your language.

flcYtb avatar

@Nerijus thank you! When I changed 'locale' => 'ja', to 'locale' => 'en', in config/app.php, the display changed to "Product6 summary"! I will try to respond by referring to the subsequent Multi-language lessons. thank you very much!!

Nerijus avatar

Check here https://github.com/filamentphp/filament#checking-for-missing-translations to see which keys are missing and make a pr to the filament

flcYtb avatar

thank you! "php artisan filament:check-translations ja" When I tried it, I found a lot of "Missing".

I've never done PR, but if I can fill it, I'll give it a try!

Thank you for your detailed guide!

Paulo Cavalcanti avatar

There is a way to export a table with the summaries? I didn't manage a way to do that. Nothing to fancy, just the sum of the columns.

Modestas avatar

Not sure about this one as I have not seen this asked in filament discord. So it is either pretty simple (with plugin or without) or it's pretty hard and nobody did it

Daniel avatar

Is order model a pivot table? i couldn't go through this. for filament the most important part are model and migration. pleace include those in the beginning

Modestas avatar

You can find the repository at our last lesson:

https://github.com/LaravelDaily/Filament3-Course-Main

Hope that helps!

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.