我是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
我希望格式化$Expense->price
和$Expense->amount
。
我试着在$Expense->amount
上以number_format($Expense->amount)
的身份使用它,但它不起作用。
发布于 2014-12-27 02:23:02
这应该是可行的:
<td>{{ number_format($Expense->price, 2) }}</td>
发布于 2016-04-01 01:58:08
如果您正在使用Eloquent,请在您的模型中放入:
public function getPriceAttribute($price)
{
return $this->attributes['price'] = sprintf('U$ %s', number_format($price, 2));
}
其中getPriceAttribute是您在数据库上的字段。getSomethingAttribute.
发布于 2016-04-28 21:50:53
如果您使用的是Eloquent,那么最好的解决方案是:
public function getFormattedPriceAttribute()
{
return number_format($this->attributes['price'], 2);
}
因此,现在您必须在模型中附加价格,并且可以同时使用formattedPrice (在其原始状态下)和formattedPrice。
https://stackoverflow.com/questions/27651808
复制相似问题