Reputation: 3197
I'm trying to format an integer in Laravel Nova but nothing seems to work.
I have tried the following:
Currency::make('Price', 'price')
->displayUsing(function($value) {
return number_format($value, ',');
})
->hideFromDetail(),
Currency::make('Price', 'price')
->format('%.2n')
->hideFromDetail(),
It also doesn't work if I utilise the Number field.
The result is always an unformatted number like 49000000000 when it should be 49,000,000,000
What am I missing?
Upvotes: 0
Views: 5859
Reputation: 1378
What you can do is using Accessors and mutators inside your Product modal instead.
Assume you have a Product modal with a price attribute
public function price(): Attribute
{
return Attribute::make(
get: fn ($value) => number_format($value, 2, '.', ','),
set: fn ($value) => str_replace(',', '', $value),
);
}
This will always format the price after it get's the value. And when you save the price it will replace the ',' and just saves the plain number.
In Nova
Number::make('Price', 'price')->min(0),
Link to documentation - Laravel 9.+
Upvotes: 0
Reputation: 87
Try this, maybe help you:
Text::make('Price', 'price', function () {
return !is_null($this->price) ? number_format($this->price, 2, '.', ',') : 0;
}),
Upvotes: 2