百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel-money

> 后端框架
开源

Laravel 财务。

1.0K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Laravel 财务。

Laravel Money

Note: This project abstracts MoneyPHP

Installation

Run the following command from you terminal:

bash
composer require cknow/laravel-money

or add this to require section in your composer.json file:

bash
"cknow/laravel-money": "^8.0"

then run composer update

Usage

php
use Cknow\Money\Money;

echo Money::USD(500); // $5.00
echo Money::USD(500, true); // $500.00 force decimals

Configuration

The defaults are set in config/money.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

bash
php artisan vendor:publish --provider="Cknow\Money\MoneyServiceProvider"

This is the contents of the published file:

…

Advanced Usage

See MoneyPHP for more information

…

Create your formatter

php
class MyFormatter implements \Money\MoneyFormatter
{
    /**
     * Formats a Money object as string.
     */
    public function format(\Money\Money $money): string
    {
        return 'My Formatter';
    }
}

Money::USD(500)->formatByFormatter(new MyFormatter()); // My Formatter

Rules

Below is a list of all available validation rules and their function:

currency

The field under validation must be a valid currency.

php
Validator::make([
  'currency1' => 'USD',
  'currency2' => 'EUR',
  'currency3' => new \Money\Currency('BRL'),
], [
  'currency1' => new \Cknow\Money\Rules\Currency(),
  'currency2' => new \Cknow\Money\Rules\Currency(),
  'currency3' => 'currency',
]);

money

The field under validation must be a valid money.

php
Validator::make([
  'money1' => '$10.00'
  'money2' => '€10.00',
  'money3' => 'R$10,00',
  'money4' => '$10.00'
  'money5' => '€10.00',
  'money6' => 'R$10,00',
], [
  'money1' => new \Cknow\Money\Rules\Money(),
  'money2' => new \Cknow\Money\Rules\Money('EUR'), // forcing currency
  'money3' => new \Cknow\Money\Rules\Money('BRL', 'pt_BR'), // forcing currency and locale
  'money4' => 'money',
  'money5' => 'money:EUR', // forcing currency
  'money6' => 'money:BRL,pt_BR', // forcing currency and locale
]);

Casts

At this stage the cast can be defined in the following ways:

…

In the example above, if the model attribute currency is null, the currency defined in the package configuration is used instead.

Setting money can be done in several ways:

php
$model->money = 10; // 0.10 USD or any other currency defined
$model->money = 10.23; // 10.23 USD or any other currency defined
$model->money = 'A$10'; // 10.00 AUD
$model->money = '1,000.23'; // 1000.23 USD or any other currency defined
$model->money = '10'; // 0.10 USD or any other currency defined
$model->money = Money::EUR(10); // 0.10 EUR

When we pass the model attribute holding the currency, such attribute is updated as well when setting money:

php
$model->currency; // null
$model->money = '€13';
$model->currency; // 'EUR'
$model->money->getAmount(); // '1300'

Helpers

…

Blade Extensions

…

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPcurrencylaravelmonet

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架