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? (30 h 09 min)

You also get:

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

Already a member? Login here

Comments & Discussion

CB
Caspar Bisschop ✓ Link copied!

Very usefull and a nice new feature in filament v3

AA
Ali Al Qahtani ✓ Link copied!

Great 👍

EJ
Emile Jobity ✓ Link copied!

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))),

PK
Povilas Korop ✓ Link copied!

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.

JW
Jeroen Willemsen ✓ Link copied!

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

N
Nerijus ✓ Link copied!

How do you sort and on what kind of field?

JW
Jeroen Willemsen ✓ Link copied!

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

F
flcYtb ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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.

F
flcYtb ✓ Link copied!

@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!!

N
Nerijus ✓ Link copied!

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

F
flcYtb ✓ Link copied!

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!

PC
Paulo Cavalcanti ✓ Link copied!

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.

M
Modestas ✓ Link copied!

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

D
Daniel ✓ Link copied!

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

M
Modestas ✓ Link copied!

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.