F
Filamentβ€’12mo ago
Quin.

Money function but what if it is empty?

Ello, In the documentation i am reading there is a money function but if the value isn't a int or float you get a error, Is there anyway if the field is empty you could disable the function or something in that way
No description
Solution:
TextColumn::make('price')->getStateUsing(fn ($record) =>
is_null($record->price) ? '-' : '€' . number_format($record->price, 2, '.', '')
),
TextColumn::make('price')->getStateUsing(fn ($record) =>
is_null($record->price) ? '-' : '€' . number_format($record->price, 2, '.', '')
),
...
Jump to solution
10 Replies
Quin.
Quin.OPβ€’12mo ago
TextColumn::make('price')->getStateUsing(fn ($record) => $record->price ?? '-')->money(),
TextColumn::make('price')->getStateUsing(fn ($record) => $record->price ?? '-')->money(),
this is my code if it is empty there will come a '-' for in the place but then i get that error
einnlleinhatt_
einnlleinhatt_β€’12mo ago
What's the error?
Quin.
Quin.OPβ€’12mo ago
No description
toeknee
toekneeβ€’12mo ago
Quin, sounds like you are storing your data as a string, so the model returns the value as a string. You need to cast price as int or float on the model.
Quin.
Quin.OPβ€’12mo ago
protected $casts = [
'status' => DomainRegistrationStatus::class,
'price' => 'float',
];
protected $casts = [
'status' => DomainRegistrationStatus::class,
'price' => 'float',
];
something like this?
toeknee
toekneeβ€’12mo ago
yeah
Quin.
Quin.OPβ€’12mo ago
doesn't change the fact that i am still using the '-' if it is empty right?
toeknee
toekneeβ€’12mo ago
It will as you can't output - in the column when using money as money is formatting it If you want to format it then remove money() and just replicate what money does in the condition instead. You are fighting between money values and a string you see
Quin.
Quin.OPβ€’12mo ago
Aah yeah that's also a option ty! will share the code when i am done]
Solution
Quin.
Quin.β€’12mo ago
TextColumn::make('price')->getStateUsing(fn ($record) =>
is_null($record->price) ? '-' : '€' . number_format($record->price, 2, '.', '')
),
TextColumn::make('price')->getStateUsing(fn ($record) =>
is_null($record->price) ? '-' : '€' . number_format($record->price, 2, '.', '')
),
Final result πŸ˜„
Want results from more Discord servers?
Add your server