Tricks

Calculated value in column

Jun 29, 2022
Juan Benitez
Table builder, Admin panel

Sometimes in your tables, you want to show a value that dependes on other model fields, e.g.:

  • difference between fields values
  • concatenation of several fields values
  • etc.

By passing a callback function to the getStateUsing() method from a Column component, you can customize the returned $state value.

Tables\Columns\TextColumn::make('myCalculatedColumn')
->getStateUsing(function(Model $record) {
// return whatever you need to show
return $record->field1 + $record->field2;
})
avatar

I'd love to see how to make this Trick also be sortable.