Dynamically create a column in the

Tables\Columns\TextColumn::make('date')
->date()
->sortable(),
Tables\Columns\TextColumn::make('user_amount')
->searchable()
->toggleable()
->sortable()
->label('Money from Safe'),
Tables\Columns\TextColumn::make('system_amount')
->searchable()
->toggleable()
->sortable()
->label('System Amount')
->sortable(),
Tables\Columns\TextColumn::make('difference')
->label('Difference')
->formatStateUsing(function (Model $record) {
die($record);
return $record->user_amount - $record->system_amount;
}),
Tables\Columns\TextColumn::make('type')
->searchable(),
Tables\Columns\TextColumn::make('date')
->date()
->sortable(),
Tables\Columns\TextColumn::make('user_amount')
->searchable()
->toggleable()
->sortable()
->label('Money from Safe'),
Tables\Columns\TextColumn::make('system_amount')
->searchable()
->toggleable()
->sortable()
->label('System Amount')
->sortable(),
Tables\Columns\TextColumn::make('difference')
->label('Difference')
->formatStateUsing(function (Model $record) {
die($record);
return $record->user_amount - $record->system_amount;
}),
Tables\Columns\TextColumn::make('type')
->searchable(),
I want to create a field called difference
12 Replies
codeartisan
codeartisanOP10mo ago
Hello members any help
bathishig
bathishig10mo ago
public function getPriceDifferenceAttribute() { return $this->price - $this->price2; } in your model Tables\Columns\TextColumn::make('price_difference') ->label('Price Difference'), in your table
LeandroFerreira
LeandroFerreira10mo ago
You can also use a virtual column: https://filamentphp.com/docs/3.x/forms/fields/checkbox-list#customizing-the-relationship-option-labels
$table->string('difference')->virtualAs('user_amount - system_amount');
$table->string('difference')->virtualAs('user_amount - system_amount');
Tables\Columns\TextColumn::make('difference')
Tables\Columns\TextColumn::make('difference')
codeartisan
codeartisanOP10mo ago
thank you
bathishig
bathishig10mo ago
virtual column look better looks
codeartisan
codeartisanOP10mo ago
I cant find any information about a virtual column first one works but the second one is giving me challenges
LeandroFerreira
LeandroFerreira10mo ago
Did you create the migration and add this column? What is the issue?
codeartisan
codeartisanOP10mo ago
no it a dynamic field but the first solution works fine just wanted to try out the second option
LeandroFerreira
LeandroFerreira10mo ago
Are you using mysql?
codeartisan
codeartisanOP10mo ago
Yeah
LeandroFerreira
LeandroFerreira10mo ago
Create a migration to your table and add this
$table->string('difference')->virtualAs('user_amount - system_amount');
$table->string('difference')->virtualAs('user_amount - system_amount');
Then, you can use this virtual column as a column in you app
Tables\Columns\TextColumn::make('difference')
Tables\Columns\TextColumn::make('difference')
codeartisan
codeartisanOP9mo ago
thank you

Did you find this page helpful?