@as3admystery did you try?
public function totalAmount(): Attribute
{
return new Attribute(
get: fn ($value) => number_format(floatval($value), 2),
);
}
Hello, I have an Invoice model with a total_amount attribute, I want to format the value and used
public function totalAmount(): Attribute
{
return new Attribute(
get: fn ($value) => number_format((float)$value, 2, '.', ','),
);
}
the problem is if value = 3047.00, the result showing is = 3.00. also if I tried number_format($value) without using the (float) before it, I get error:
number_format(): Argument #1 ($num) must be of type float, string given
@As3adMystery did you try with a method?
public function totalAmountFormatted()
{
return number_format(floatval($this->total_amount), 2);
}
And then use as:
$item->totalAmountFormatted();
Please or to participate in this conversation.