ez-laravel / currencies
简单的Laravel货币与转换
Requires
- ez-laravel/model-services: ^1.0
- guzzlehttp/guzzle: ^6.5
- illuminate/support: ^7.0
This package is not auto-updated.
Last update: 2024-09-27 19:30:21 UTC
README
此软件包为您提供了在应用程序中使用货币以及轻松检索汇率并将其应用于价格的功能。
它使用了外部API,其中一些API需要API密钥。您可以在下面的说明中找到更多详细信息。
安装
在您的项目目录中运行以下命令以安装此软件包
composer require ez-laravel/currencies
使用以下命令发布迁移和种子
php artisan vendor:publish --provider="EZ\Currencies\Providers\CurrenciesServiceProvider" --tag=database
使用以下命令运行迁移
php artisan migrate
更新您的 DatabaseSeeder.php
类以加载新的种子,然后运行以下命令以填充您的数据库
composer dumpautoload
php artisan db:seed
运行以下命令以(可选)发布软件包的配置文件
php artisan vendor:publish --provider="EZ\Currencies\Providers\CurrenciesServiceProvider" --tag=config
配置
默认货币
默认货币设置为EUR,您可以通过在 .env
文件中添加以下键并指定所需的货币代码来更改它
CURRENCIES_DEFAULT=USD
货币转换汇率API
以下API受支持或正在开发中,以供支持
您可以通过在 .env
文件中添加以下键来更改软件包将使用的驱动程序
CURRENCIES_API_DRIVER=fixer
Fixer
fixer
从 fixer.io 网站 获取API密钥并将其添加到您的 .env
文件中
CURRENCIES_FIXER_API_KEY=xxxxxxxx
RatesAPI
rates
ratesapi.io 网站 不需要API密钥!
Frankfurter
frankfurter
frankfurter.app 网站 不需要API密钥!
ExchangeRatesAPI
exchangerates
exchangeratesapi.io 网站 不需要API密钥!
用法
可用方法
// Currency getters $currencies = Currencies::getAll(); $currency = Currencies::find($id); $currency = Currencies::findBy($field, $value); $currency = Currencies::findByCode($code); $num_currencies = Currencies::countAll(); // Preloaded currency getters $currencies = Currencies::getAllPreloaded(); $currency = Currencies::findPreloaded($id); $currency = Currencies::findPreloadedBy($field, $value); // Currency conversion rate getters $conversionRates = Currencies::getConversionRates(); $conversionRates = Currencies::getConversionRatesByCode($code); // Update all currency conversion rates Currencies::updateConversionRates(); // Conversion methods $convertedValue = Currencies::convert($fromCurrency, $toCurrency, $x); $convertedValues = Currencies::convertToAll($fromCurrency, $x);
更新转换率
刚安装软件包时,您应该执行以下命令以手动更新所有货币的转换率
php artisan currencies:update-conversion-rates
要自动保持转换率更新,请将上述命令安排每天(或您希望的任何间隔)运行,通过更新您的 app/Console/Kernel.php
文件以包含以下内容
protected function schedule(Schedule $schedule) { ... $schedule->command('currencies:update-conversion-rates')->daily(); }
扩展货币模型
在大多数应用程序中,您可能希望创建货币模型与产品模型或订单模型之间的关系。为此,只需创建自己的货币模型,该模型扩展了 EZ\Currencies\Models\Currency
模型,并在 currencies.php
配置文件中更新模型路径。
例如
<?php namespace App\Models; use EZ\Currencies\Models\Currency as BaseCurrency; class Currency extends BaseCurrency { public function products() { return $this->hasMany(Product::class); } }
在 currencies.php
中
... 'model' => App\Models\Currency::class, ...
贡献
如果您想做出贡献,请自由提交带有您驱动程序或其他改进的PR请求!任何其他反馈都是受欢迎的。