F
Filament11mo ago
Connor

How to add a sum of related fields in Form/Components/section

So I have a side panel next to my Leads form,
Forms\Components\Section::make()->schema([
Forms\Components\Placeholder::make('created_at')
->label('Created at')
->content(fn (Lead $record): ?string => $record->created_at?->diffForHumans()),

Forms\Components\Placeholder::make('updated_at')
->label('Last modified at')
->content(fn (Lead $record): ?string => $record->updated_at?->diffForHumans()),

// sum of related services
])
Forms\Components\Section::make()->schema([
Forms\Components\Placeholder::make('created_at')
->label('Created at')
->content(fn (Lead $record): ?string => $record->created_at?->diffForHumans()),

Forms\Components\Placeholder::make('updated_at')
->label('Last modified at')
->content(fn (Lead $record): ?string => $record->updated_at?->diffForHumans()),

// sum of related services
])
The Lead resource hasMany Services. I would like to summarise the value of all these services by 'price' in the side panel next to my lead form. Is this possible and if so, what should I be looking at to figure this out? Appreciate the help!
No description
2 Replies
BlackShadow
BlackShadow11mo ago
You want the count of all the services? @cnnrp
// Count
->content(fn (Lead $record) => $record->services->count())
// Count
->content(fn (Lead $record) => $record->services->count())
// Sum by price
->content(fn (Lead $record) => $record->services->sum('price'))
// Sum by price
->content(fn (Lead $record) => $record->services->sum('price'))
Connor
Connor11mo ago
Oh right, so I can just reference the relationship from this record. I was expecting something more difficult. That's perfect thank you.