在拉拉维尔使用number_format方法

2022-08-30 11:45:18

我在Laravel和Blade模板中相当陌生。
任何人都可以帮我教我如何做到这一点吗?

我有这样一个观点:

@foreach ($Expenses as $Expense)
    <tr>
        <td>{{{ $Expense->type }}}</td>
        <td>{{{ $Expense->narration }}}</td>
        <td>{{{ $Expense->price }}}</td>
        <td>{{{ $Expense->quantity }}}</td>
        <td>{{{ $Expense->amount }}}</td>                                                            
    </tr>
@endforeach

我希望 和 被格式化。
我尝试在as上使用它,但它不起作用。$Expense->price$Expense->amount$Expense->amountnumber_format($Expense->amount)


答案 1

这应该有效:

<td>{{ number_format($Expense->price, 2) }}</td>

答案 2

如果您使用的是 Eloquent,请在您的模型中放置:

public function getPriceAttribute($price)
{
    return $this->attributes['price'] = sprintf('U$ %s', number_format($price, 2));
}

其中“获取价格属性”是您在数据库中的字段。获取某些属性


推荐