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
codeartisanOP8mo ago
Hello members any help
bathishig
bathishig8mo 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
LeandroFerreira8mo 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
codeartisanOP8mo ago
thank you
bathishig
bathishig8mo ago
virtual column look better looks
codeartisan
codeartisanOP8mo ago
I cant find any information about a virtual column first one works but the second one is giving me challenges
LeandroFerreira
LeandroFerreira8mo ago
Did you create the migration and add this column? What is the issue?
codeartisan
codeartisanOP8mo ago
no it a dynamic field but the first solution works fine just wanted to try out the second option
LeandroFerreira
LeandroFerreira8mo ago
Are you using mysql?
codeartisan
codeartisanOP8mo ago
Yeah
LeandroFerreira
LeandroFerreira8mo 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
codeartisanOP7mo ago
thank you
Want results from more Discord servers?
Add your server