首页 > 解决方案 > 是否可以在 Voyager 中格式化显示的数据?

问题描述

我是Voyager的新手,我想将一些数字格式化为货币格式。通常,我会这样做

//In a blade file {{ $number_format($product->price, 0, ',', '.') }}

如果我想在 BREAD 视图中格式化显示的数据,我的 JSON 字符串应该如何实现我的目的?

标签: phplaravel-5voyager

解决方案


您可以使用Eloquent 访问器

在您的实例中,这可能如下所示:

<?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, ',', '.');
        }
    }

推荐阅读