【发布时间】:2018-07-09 14:48:57
【问题描述】:
我是Voyager 的新手,我想将一些数字格式化为货币格式。通常,我会这样做
//In a blade file
{{ $number_format($product->price, 0, ',', '.') }}
如果我想在 BREAD 视图中格式化显示的数据,我的 JSON 字符串应该如何实现我的目的?
【问题讨论】:
我是Voyager 的新手,我想将一些数字格式化为货币格式。通常,我会这样做
//In a blade file
{{ $number_format($product->price, 0, ',', '.') }}
如果我想在 BREAD 视图中格式化显示的数据,我的 JSON 字符串应该如何实现我的目的?
【问题讨论】:
您可以使用Eloquent accessors。
在您的实例中,这可能如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* Get the product's price.
*
* @param string $value
* @return string
*/
public function getPriceAttribute($value)
{
return number_format($value, 0, ',', '.');
}
}
【讨论】: