Perhaps there is a better way to do the following but I would like a bit of rectoring to know what is wrong. I am trying to create a blade directive which can format weights or dollars, I mean that if we give an amount like 35000 this directive will be responsible for formatting it in the following way 35,000, to make it more readable, the problem is I'm not getting the results I expected, this is the code in the AppServiceProvider:
Blade::directive('formatodinero', function ($precio) {
$precio = strrev($precio);
$tamanio = strlen($precio);
$nuevoPrecio = "";
$j = -1;
for($i = $tamanio; $i > 0; $i--){
if($j % 3 == 0){
$nuevoPrecio .= ',';
}
$nuevoPrecio .= $precio[$i-1];
$j++;
}
return "<?php echo ($nuevoPrecio) ?>";
});
And the way I call the directive from my view is this:
@formatodinero($propiedad->precio_venta)
Instead of printing the result formatted with commas I'm getting the string that happened to the directive itself.