To format fields of a model in Laravel
, use Accessors and mutators , that is, the typical (get and set) , so if you want to format type Moneda
the function you are using would be correct but you should keep in mind that this function is not available in some Operating Systems.
Instead, you could use number_format to give the result you want.
For this in your model I would define the getter , for this example I will use only two fields name and price
protected $fillable = [
'nombre','precio'
];
/* después del get debe ir el nombre del campo seguido de Attribute*/
public function getPrecioAttribute($value) {
return '$'.number_format($value, 2, ',', '');
}
Having this defined every time you want to obtain the attribute of the class will apply the respective format, from the view you would access normally.
@foreach ($productos as $element)
<li><p>{{ $element->precio }}</p></li>
@endforeach
PD: There may be better ways to present the data in the view