I have the table products
with price
column. In model I would like to do something like this:
public function getPriceAttribute()
{
return number_format($this->price);
}
So in view I use
{{ $property->price }} €
and get the value 200
instead 200.00
how is decimal from database.
Is this possible?
This is what solved my problem:
public function getPriceAttribute()
{
return number_format($this->attributes['price']);
}
This will overwrite the $property->price
value (as per comments)
You can do so by passing original value as argument, like this:
public function getPriceAttribute($price)
{
return number_format($price);
}
You can find more about Mutators (and Casting) here: https://laravel.com/docs/8.x/eloquent-mutators
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With