restuniverse/currency

此包提供了Laravel函数,例如使用最新的汇率进行货币格式化和转换。

v2.3 2022-08-01 06:03 UTC

This package is auto-updated.

Last update: 2024-09-25 10:41:18 UTC


README

安装

Composer

从命令行运行

$ composer require restuniverse/currency

Laravel的 >=5.5 自动发现

只需安装此包,然后让Laravel施展魔法。

手动设置

安装后,您需要将服务提供者注册到应用程序中。打开 config/app.php 并找到 providers 键。

'providers' => [

    \RestUniverse\Currency\CurrencyServiceProvider::class,

]

此包还附带一个外观,它提供了调用类的一种简单方法。打开 config/app.php 并找到 aliases 键。

'aliases' => [

    'Currency' => \RestUniverse\Currency\Facades\Currency::class,

];

发布配置

在项目根目录中运行此命令行

php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=config

配置文件将发布到 config/currency.php

迁移

如果要将货币存储在数据库中。

php artisan vendor:publish --provider="RestUniverse\Currency\CurrencyServiceProvider" --tag=migrations

从项目根目录运行此命令行以生成存储货币的表

$ php artisan migrate

注意:使用(REST_UNIVERSE_API_KEY)将API_KEY添加到.env文件中。

基本用法

使用这些方法的简单方法是通过辅助函数 currency() 或使用外观。以下示例将使用辅助方法。

currency($amount, $from = null, $to = null, $format = true)

参数

$amount - 要转换的浮点金额 $from - 金额的当前货币代码。如果未设置,则使用应用程序默认设置(请参阅 config/currency.php 文件)。 $to - 要转换金额到的货币代码。如果未设置,则使用用户设置的货币。 $format - 返回值是否需要格式化。

用法

echo currency(12.00);               // Will format the amount using the user selected currency
echo currency(12.00, 'USD', 'EUR'); // Will format the amount from the default currency to EUR

更新汇率

从restuniverse.com更新汇率。使用Rest Universe需要API密钥。将其添加到配置文件中。

php artisan currency:hydrate

安全

如果您发现任何与安全相关的问题,请通过电子邮件 security@restuniverse.com 联系,而不是使用问题跟踪器。