snoeren-development / laravel-currency-casting
将存储为整数的货币属性自动转换为浮点数。
v1.9.0
2024-03-25 07:37 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- illuminate/database: ^v11.0.8
- phpunit/phpunit: ^11.0.8
README
此包为Laravel模型添加了类型转换功能。这样,您可以将任何存储在数据库中的整数货币值自动转换为浮点数!
安装
您可以使用Composer安装此包
composer require snoeren-development/laravel-currency-casting
要求
此包需要至少PHP 8.2和Laravel 10。
用法
将您的货币作为整数值存储在数据库中。这比将其存储为浮点数更准确。将您希望转换的属性添加到casts
数组中,并将其分配给Currency
类。如果您需要的货币位数超过默认的2位,您可以在示例中像下面那样在货币类之后追加所需的位数。只需确保您的数据库列可以处理产生的更大整数即可。
<?php use Illuminate\Database\Eloquent\Model; use SnoerenDevelopment\CurrencyCasting\Currency; class Plan extends Model { // /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'price' => Currency::class, 'price_with_digits' => Currency::class . ':4', ]; // }
测试
$ composer test
致谢
许可协议
MIT许可协议。有关更多信息,请参阅LICENSE。