summarize and custom model attribute
Hi
I have a model attribute:
protected function sumRetail(): Attribute
{
return Attribute::make(
get: fn () => $this->price_retail * $this->qty
);
}
And I would like to summarize the sum_retail column.
With this notation, summarize does not work, but row summation works:
TablesColumnsTextColumn::make('sum_retail')->label('Value.')->alignRight()
->summarize(Sum::make()->numeric(decimalPlaces: 2)),
With this notation, summarize works, but row summation does not work.
TablesColumnsTextColumn::make('price_retail * qty')->label('Value')->alignRight()
->summarize(Sum::make()->numeric(decimalPlaces: 2)),
So how should this be correct?4 Replies
I am having the same issue.
@qcol74 I think a custom version of sum is required.
I'm going to try and write one and will let you know how I get on.
@qcol74 Here is the attribute which will do what you want.
Solution
Thank you for this advice. I also found this solution:
->summarize(
Summarizer::make()
->using(fn (Builder $query): string => $query->sum(DB::Raw('price_retail*qty')))
->numeric(decimalPlaces: 2)
)
I'm not sure if they example custom sum above was for Filament
v2.x
or not, but it wasn't working properly on my v3.x
, and was taking all models, not just those limited to the table I was displaying.
Here's a different version based off the above example which I used for formatting the sums of Attributes for a money column, but that can easily be changed according to your needs.