F
Filamentβ€’6mo 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.β€’6mo 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_β€’6mo ago
What's the error?
Quin.
Quin.β€’6mo ago
No description
toeknee
toekneeβ€’6mo 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.β€’6mo ago
protected $casts = [
'status' => DomainRegistrationStatus::class,
'price' => 'float',
];
protected $casts = [
'status' => DomainRegistrationStatus::class,
'price' => 'float',
];
something like this?
toeknee
toekneeβ€’6mo ago
yeah
Quin.
Quin.β€’6mo ago
doesn't change the fact that i am still using the '-' if it is empty right?
toeknee
toekneeβ€’6mo 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.β€’6mo ago
Aah yeah that's also a option ty! will share the code when i am done]
Solution
Quin.
Quin.β€’6mo 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 πŸ˜„