chrgriffin/eloquent-moneyphp
Composer 安装命令:
composer require chrgriffin/eloquent-moneyphp
包简介
Automatically cast Eloquent columns to MoneyPHP objects.
README 文档
README
Eloquent-MoneyPHP
Automatically cast Eloquent columns to MoneyPHP objects.
Installation
Install Eloquent-MoneyPHP with composer:
composer install chrgriffin/eloquent-moneyphp
Requirements
- PHP >= 7.1.3
- Laravel >= 5.6
This package does make one key assumption: that you are storing money in your database as integers, not floating point values. For example, eight dollars would be stored as 800, instead of 8.00. To find out why you should store currency and other floating point values this way, read about the classic problem here.
Usage
Usage is extremely simple. Eloquent-MoneyPHP provides a trait that can be used on any Eloquent model in conjunction with an array of column names:
<?php namespace App; use EloquentMoneyPHP\HasCurrency; class MyModel extends Model { use HasCurrency; protected $currencies = [ 'total_usd' => 'USD', 'total_eur' => 'EUR' ]; }
In the above setup, accessing the total_usd or total_eur attribute will automatically convert the attribute to a MoneyPHP object:
<?php $model = MyModel::find(1); $total = $model->total_usd; // <-- this will return a MoneyPHP object
Eloquent-MoneyPHP also supports storing a money amount as a json string in a text column.
"{\"amount\": 800,\"currency\": \"USD\"}"
Then configure your model appropriately:
<?php namespace App; use EloquentMoneyPHP\HasCurrency; class MyModel extends Model { use HasCurrency; protected $currencies = [ 'total' => 'json' ]; }
Under the Hood
Eloquent-MoneyPHP makes use of the Laravel magic methods getAttribute() and setAttribute() in conjunction with the configured array of column names to determine if it should cast a given attribute to a MoneyPHP object.
This could obviously prove problematic if you are already implementing getAttribute() or setAttribute() yourself. Luckily, you can include the package behaviour in the methods yourself, if you need to:
<?php namespace App; use EloquentMoneyPHP\HasCurrency; class MyModel extends Model { use HasCurrency; protected $currencies = [ 'total' => 'json' ]; public function getAttribute($key) { if($this->attributeIsMoney($key)) { return $this->getMoneyAttribute($key); } // the rest of your logic } public function setAttribute($key, $value) { if($this->attributeIsMoney($key)) { return $this->setMoneyAttribute($key, $value); } // the rest of your logic } }
chrgriffin/eloquent-moneyphp 适用场景与选型建议
chrgriffin/eloquent-moneyphp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrgriffin/eloquent-moneyphp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrgriffin/eloquent-moneyphp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-17