giordanolima/decimal-mutators
最新稳定版本:1.1.4
Composer 安装命令:
composer require giordanolima/decimal-mutators
包简介
Add a short way to create accessors and mutators for decimal fields
关键字:
README 文档
README
Decimal Mutators for Laravel
Decimal Mutators provides a short way to create accessors and mutators for decimal fields.
Install
Install package through Composer
composer require giordanolima/decimal-mutators
Usage
You should use it as a trait of your model, and declare which fields you want to apply the mutators:
use Illuminate\Database\Eloquent\Model, GiordanoLima\DecimalMutators\DecimalMutators; class MyModel extends Model { use DecimalMutators; protected $decimalsFields = [ 'decimal_field_1', 'decimal_field_2', 'decimal_field_3', 'decimal_field_4' ]; }
By default, the trait will get the data from database and will replace "," (comma) as thousand separator to ""(blank) and will replace "." (dot) as decimal separator to "," (comma). The behavior will be like this:
$myModel = MyModel::find(1); $myModel->decimal_field_1 = '200,00'; $myModel->save(); // It will store as 200.00 $myModel = MyModel::find(1); echo $myModel->decimal_field_1; // Will print 200,00
By default, it gonna be used 2 for decimal points... If you need change it, you can set the option:
protected $decimalsOptions = [ "decimals" => 4, // now, the fields will be stored and printed with 4 decimals point ];
If you want to replace defaults separators, you can replace with:
protected $decimalsOptions = [ "setDecimalsFrom" => ",", "setDecimalsTo" => ".", "setThounsandFrom" => ".", "setThounsandTo" => "", "getDecimalsFrom" => ".", "getDecimalsTo" => ",", "getThounsandFrom" => ",", "getThounsandTo" => "", ];
You can disable the mutators:
MyModel::$disableGetMutator = true; echo $myModel->decimal_field_1; // Will print 200.00 MyModel::$disableGetMutator = false; echo $myModel->decimal_field_1; // Will print 200,00
MyModel::$disableSetMutator = true; $myModel->decimal_field_1 = '200,00'; $myModel->save(); // It will store as 200,00 MyModel::$disableSetMutator = false; $myModel->decimal_field_1 = '200,00'; $myModel->save(); // It will store as 200.00
统计信息
- 总下载量: 1.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-08