ibrand/laravel-currency-format

iBrand coterie server.

v1.0.0 2018-12-27 13:09 UTC

This package is auto-updated.

Last update: 2021-04-10 09:59:32 UTC


README

###laravel-currency-format

Laravel 贴合实际需求同时方便扩展的数字,金额格式化组件

基于业务需求,需要把数字,金额格式化以方便前端展示,laravel Eloquent官方提供了修改器,如将以为单位的amount字段换算成

public function getAmountAttribute()
{
	return number_format($this->value, 2, '.', '');
}

Featrue

  1. 自定义需要转换的字段
  2. Trait即引即用
  3. FormatContract方便扩展开发
  4. 支持toArray数组化

安装

composer require ibrand/laravel-currency-format

使用

  1. 定义Eloquent Model模型
  2. 引用 格式化Trait模型: use iBrand\Currency\Format\HasFormatAttributesTrait
  3. 自定义格式化字段:protected $format_attributes

就是这么简单

<?php

/*
 * This file is part of ibrand/laravel-currency-formatter.
 *
 * (c) iBrand <https://www.ibrand.cc>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iBrand\Currency\Format;

use iBrand\Currency\Format\HasFormatAttributesTrait;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasFormatAttributesTrait;//引用格式化Trait

    protected $guarded = ['id'];

    protected $format_attributes = ['total', 'items_total'];//自定义需要格式化的字段,就是这么简单

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
      
      	$this->setTable(config('ibrand.app.database.prefix', 'ibrand_').'order');
    }
}

如访问格式化total属性,$order->display_total即可。