Reputation: 1700
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?
Upvotes: 16
Views: 31615
Reputation: 204
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
Upvotes: 4
Reputation: 1700
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)
Upvotes: 28