F
Filament10mo ago
Lederp

Make additional column in table based from the sum of two other columns?

Hi, is there a nicer way to make an additional column in a table which is based of the sum of two other columns? The idea for this is so I can then use this column as a summary, and to be sortable etc. I haven't had much luck. I've got as far as overriding the query, which allows me to create the column, but since this column doesn't exist in the database it can't be used to be sorted nor summarized.
1 Reply
Lederp
Lederp10mo ago
Tables\Columns\TextColumn::make('total_cost')->label('Total Cost')->state(
function (TrafficLogs $record) {
$totalBytes = ($record->bytes_downloaded + $record->bytes_uploaded) / (1024*1024*1024);
$type = explode("_", $record->country)[1];
return "$" . round(cost($type, $totalBytes), 2);
}
Tables\Columns\TextColumn::make('total_cost')->label('Total Cost')->state(
function (TrafficLogs $record) {
$totalBytes = ($record->bytes_downloaded + $record->bytes_uploaded) / (1024*1024*1024);
$type = explode("_", $record->country)[1];
return "$" . round(cost($type, $totalBytes), 2);
}
My current plan to be able to summarize is like so:
)->summarize(Summarizer::make()->label('Total Cost')->using(
function (Table $table) {
dd($table->getColumns()['total_cost']);
//echo `<script>console.log($price)</script>`;
//dd($table->getColumn('total_cost')->getState());
}
)),
)->summarize(Summarizer::make()->label('Total Cost')->using(
function (Table $table) {
dd($table->getColumns()['total_cost']);
//echo `<script>console.log($price)</script>`;
//dd($table->getColumn('total_cost')->getState());
}
)),
but I cannot work out how to get each row in the table for this