F
Filamentβ€’6mo ago
ddoddsr

Aggregating relationships use two sums and calculate the percentage.

I have two columns working for the Aggregating Relation but I don't see how to use the results in a third column calculation.
Tables\Columns\TextColumn::make('leads_sum_connected')
->sum('leads', 'connected')
->label('Connected'),

Tables\Columns\TextColumn::make('leads_sum_connection_request')
->sum('leads', 'connection_request')
->label('Connect Req'),
Tables\Columns\TextColumn::make('leads_sum_connected')
->sum('leads', 'connected')
->label('Connected'),

Tables\Columns\TextColumn::make('leads_sum_connection_request')
->sum('leads', 'connection_request')
->label('Connect Req'),
How to calc percent using the two above into connected_percent?
leads_sum_connected / leads_sum_connection_reques
leads_sum_connected / leads_sum_connection_reques

Tables\Columns\TextColumn::make('connected_percent')
->label('Connected %'),
Tables\Columns\TextColumn::make('connected_percent')
->label('Connected %'),
Solution:
Hi there
Jump to solution
3 Replies
Solution
Tally
Tallyβ€’6mo ago
Hi there
Tally
Tallyβ€’6mo ago
You could use:
Tables\Columns\TextColumn::make('connected_percent')
->state(function($record){
return $record->leads->sum('connected') / $record->leads->sum('connection_request');
}),
Tables\Columns\TextColumn::make('connected_percent')
->state(function($record){
return $record->leads->sum('connected') / $record->leads->sum('connection_request');
}),
beware of division by 0 πŸ™‚
ddoddsr
ddoddsrOPβ€’6mo ago
Had to try it! works great. I added .01 to the Divisor to avoid divide by zero. That will be close enough.
Want results from more Discord servers?
Add your server