count of field on relationship

Good day all. I am trying to show a field in a table which lists a total amount (integer) of a field on a relationship. I got "Tables\Columns\TextColumn::make('stocks.amount'),", which works, but it shows 100, 200, 500 etc. I just want to display the single total count of those, instead of individual numbers of each relation record. I have read the docs, but i seem unable to find a correct answer for it. Im sure im missing something, or have overlooked it perhaps? But if someone could enlighten me, i would be grateful!
2 Replies
Daniel Plomp
Daniel Plomp15mo ago
I think this should work?
TextColumn::make('stocks_sum_amount')->sum('stocks', 'amount')
TextColumn::make('stocks_sum_amount')->sum('stocks', 'amount')
It is explained here: https://filamentphp.com/docs/3.x/tables/columns/relationships#aggregating-relationships
managoresh
managoreshOP15mo ago
Works perfectly! I did try this, but my error seems to have been that i did not rename the field from stocks.amount. Thank you!
Want results from more Discord servers?
Add your server