PHPackages                             joseftraxler/laravel-money - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. joseftraxler/laravel-money

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

joseftraxler/laravel-money
==========================

Lightweight money and currency utilities for Laravel applications.

v1.1.0(3w ago)021MITPHPPHP &gt;=8.4CI passing

Since Apr 25Pushed 3w agoCompare

[ Source](https://github.com/joseftraxler/laravel-money)[ Packagist](https://packagist.org/packages/joseftraxler/laravel-money)[ Docs](https://github.com/JosefTraxler/laravel-money)[ RSS](/packages/joseftraxler-laravel-money/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (7)Versions (22)Used By (0)

Laravel Money
=============

[](#laravel-money)

[![Latest Version on Packagist](https://camo.githubusercontent.com/088ee81bb56ed791c6a06881ab85eab7247d96838f6db16f93141ff7209749ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f736566747261786c65722f6c61726176656c2d6d6f6e65792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joseftraxler/laravel-money)[![Total Downloads](https://camo.githubusercontent.com/f23539d443fa189b2b71f0cd3f3061692cefd0f46d1713147d03fd2a76495e6d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f736566747261786c65722f6c61726176656c2d6d6f6e65792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joseftraxler/laravel-money)[![PHP Version](https://camo.githubusercontent.com/4fc9b2b2def3086c8c17ad0da3a54ef454a9448d6ed816da7837f1bfb5212a73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342532422d3737374242343f7374796c653d666c61742d737175617265266c6f676f3d706870)](https://camo.githubusercontent.com/4fc9b2b2def3086c8c17ad0da3a54ef454a9448d6ed816da7837f1bfb5212a73/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342532422d3737374242343f7374796c653d666c61742d737175617265266c6f676f3d706870)[![Laravel Version](https://camo.githubusercontent.com/1935ba84f80d8fd0428eeb3f169ff5e17470fcee31ea645e9eceff09797937be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322532422d4646324432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c)](https://camo.githubusercontent.com/1935ba84f80d8fd0428eeb3f169ff5e17470fcee31ea645e9eceff09797937be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322532422d4646324432303f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c)[![License](https://camo.githubusercontent.com/6c711032aff1ca0eb6b211aa6cb3649ce7fd64a7714e1181d4bb457f9680e7cf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/6c711032aff1ca0eb6b211aa6cb3649ce7fd64a7714e1181d4bb457f9680e7cf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666c61742d737175617265)[![CI](https://github.com/joseftraxler/laravel-money/actions/workflows/CI.yml/badge.svg)](https://github.com/joseftraxler/laravel-money/actions/workflows/CI.yml)

A lightweight Laravel package for working with money values, currencies, formatting, arithmetic operations, and Eloquent casts.

Laravel Money provides a small immutable `Money` value object with currency-aware precision handling and convenient integration for Laravel applications.

Features
--------

[](#features)

- Immutable `Money` value object
- ISO 4217-style currency code validation
- Currency-specific precision support
- Decimal and cents-based factory methods
- Arithmetic operations powered by [`moneyphp/money`](https://github.com/moneyphp/money)
- Human-readable formatting using PHP `intl`
- Eloquent casts for decimal and cents database columns
- Support for fixed currency and currency-column casts
- Laravel auto-discovery
- Publishable configuration

Requirements
------------

[](#requirements)

- PHP 8.4+
- Laravel 12+
- PHP `intl` extension

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require joseftraxler/laravel-money
```

The service provider is registered automatically through Laravel package discovery.

Configuration
-------------

[](#configuration)

The package works without publishing configuration. If you want to customize default currency or precision settings, publish the config file:

```
php artisan vendor:publish --tag=money-config
```

This publishes: [config/money.php](config/money.php)

Example configuration:

```
return [
    'default_currency' => 'USD', // default currency used by casts when no currency is provided
    'default_precision' => null, // null = automatically detected
    'currencies' => [
        'JPY' => ['precision' => 0],
        'BHD' => ['precision' => 3],
    ],
];
```

Basic usage
-----------

[](#basic-usage)

```
use JosefTraxler\LaravelMoney\Money;

$money = Money::EUR('10.45');
$money->getDecimal(); // "10.45"
$money->getCents(); // "1045"
$money->getCurrency()->getCode(); // "EUR"

$twoPieces = $money->multiply(2)->getDecimal(); // "20.90"

$product->price = $money;
$product->save();

$product->price->toHumanReadable(locale: 'en_US'); // e.g.: "€10.45"
```

In Blade:

```
{{ $product->price }}
```

Currency codes are strict ISO 4217-style codes. They must contain exactly three letters.

### Currency precision

[](#currency-precision)

Precision is resolved in this order:

1. `money.currencies.{CODE}.precision`
2. `money.default_precision`
3. package-detected default precision

Precision values are validated and cached per currency for performance.

> ⚠️ When a decimal value contains more fractional digits than the currency precision allows, the value is truncated to the configured precision.

Creating money values
---------------------

[](#creating-money-values)

### From decimal

[](#from-decimal)

```
use JosefTraxler\LaravelMoney\Currency;
use JosefTraxler\LaravelMoney\Money;

new Money('123.45', 'EUR');
Money::fromDecimal('123.45', 'EUR');
Money::EUR('123.45');
Money::fromDecimal('123.45', Currency::of('EUR'));
```

### From cents

[](#from-cents)

```
Money::fromCents(12345, 'EUR'); // 123.45 EUR
```

### Numeric strings are recommended

[](#numeric-strings-are-recommended)

The package accepts integers, floats, and numeric strings for convenience:

```
Money::fromDecimal(10, 'EUR');
Money::fromDecimal(10.50, 'EUR');
Money::fromDecimal('10.50', 'EUR'); // recommended
```

For exact monetary input, prefer numeric strings:

```
Money::fromDecimal('10.50', 'EUR');
```

### Using custom currency class/enum

[](#using-custom-currency-classenum)

You can also use a custom currency class/enum that implements `JosefTraxler\LaravelMoney\Contracts\Currencyable`:

```
enum MyCurrency: string implements JosefTraxler\LaravelMoney\Contracts\Currencyable
{
    case EUR = 'EUR';
    case USD = 'USD';

    public function getCode(): string
    {
        return $this->value;
    }
}

Money::fromDecimal('123.45', MyCurrency::EUR);
```

or some model e.g.:

```
class Currency extends Model implements JosefTraxler\LaravelMoney\Contracts\Currencyable
{
    public function getCode(): string
    {
        return $this->code;
    }
}

$currency = new Currency(['code' => 'EUR']);
Money::fromDecimal('123.45', $currency);
```

Arithmetic
----------

[](#arithmetic)

Money values support common arithmetic operations.

```
$ten = Money::EUR('10.00');
$fiveFifty = Money::EUR('5.50');
$ten->add($fiveFifty)->getDecimal(); // "15.50"
$ten->subtract($fiveFifty)->getDecimal(); // "4.50"
$ten->multiply(2)->getDecimal(); // "20.00"
$ten->divide(2)->getDecimal(); // "5.00"
```

Arithmetic operations are currency-aware and delegated to `moneyphp/money`.

Comparisons
-----------

[](#comparisons)

```
$ten = Money::EUR('10.00');
$twenty = Money::EUR('20.00');
$ten->equals($twenty); // false
$ten->lessThan($twenty); // true
$twenty->greaterThan($ten); // true
$ten->compare($twenty); // -1
```

Predicates are also available:

```
Money::EUR('10.00')->isPositive(); // true
Money::EUR('-10.00')->isNegative(); // true
Money::EUR('0.00')->isZero(); // true
```

Formatting
----------

[](#formatting)

In Laravel Blade, money values are automatically formatted according to the current locale.

For example, in `en_US`, this may render as `€12.35`:

```
{{ Money::EUR('12.35') }}
```

Formatting uses PHP `NumberFormatter`, so the `intl` extension is required.

You can specify a custom locale:

```
{{ Money::EUR('12.34')->toHumanReadable(locale: 'cs_CZ') }}
```

For `cs_CZ`, this may render as `12,34 €`.

Eloquent casts
--------------

[](#eloquent-casts)

Laravel Money provides casts for storing money values as decimal or cents.

Cast definitions are specified by:

- currency code source
    - fixed: defined by a three-letter uppercase currency code
    - dynamic: defined by a currency column name
- amount source
    - decimal: amount in major units
    - cents: amount in minor units

### Fixed currency decimal cast

[](#fixed-currency-decimal-cast)

Use this when the database column stores only the decimal amount and the currency is fixed:

```
use Illuminate\Database\Eloquent\Model;
use JosefTraxler\LaravelMoney\Money;

class Product extends Model
{
    protected function casts(): array
    {
        return [
            'price' => Money::class . ':EUR',
        ];
    }
}
```

Usage:

```
$product = Product::find(1);
$product->price->getDecimal(); // "19.99"
$product->price->getCurrency()->getCode(); // "EUR"
```

> ⚠️ When using a fixed currency cast, assigning a Money instance with a different currency throws MoneyMismatchCurrencyException.

### Dynamic currency column

[](#dynamic-currency-column)

Use this when the amount and currency are stored in separate columns:

```
use Illuminate\Database\Eloquent\Model;
use JosefTraxler\LaravelMoney\Money;

class Order extends Model
{
    protected function casts(): array
    {
        return [
            'price' => Money::class . ':currency',
            'currency' => 'string',
        ];
    }
}
```

Usage:

```
$order = new Order();
$order->price = Money::USD('19.99');
$order->price->getDecimal(); // "19.99"
$order->price->getCurrency()->getCode(); // "USD"
$order->currency; // "USD"
```

When using a dynamic currency column, the package stores the currency code automatically.

When assigning `null` to a money attribute using a dynamic currency column, only the amount column is set to `null`. The currency column is preserved intentionally, because the same currency column may be shared by multiple money attributes.

### Cents storage

[](#cents-storage)

If you store money as cents, pass `cents` as the second-cast argument:

```
use Illuminate\Database\Eloquent\Model;
use JosefTraxler\LaravelMoney\Money;

class Payment extends Model
{
    protected function casts(): array
    {
        return [
            'amount_in_cents' => Money::class . ':currency,cents',
            'currency' => 'string',
        ];
    }
}
```

The example below stores `1999` in `amount_in_cents` and `"EUR"` in `currency`:

```
$payment = new Payment();
$payment->amount_in_cents = Money::EUR('19.99');
$payment->save();
```

When retrieving the value, the package automatically converts cents to `Money` using `Money::fromCents()`.

Design notes
------------

[](#design-notes)

### Strict currency codes

[](#strict-currency-codes)

Currency codes are validated as three-letter ISO 4217-style codes. Whitespace is not trimmed automatically. This is intentional: invalid or unnormalized input should be handled before creating a currency value.

### Precision caching

[](#precision-caching)

Currency precision is resolved from configuration and cached per currency code. This keeps the package efficient when many money objects are created during a request.

### Decimal normalization

[](#decimal-normalization)

Money amounts are normalized according to currency precision:

```
Money::EUR('10')->getDecimal(); // "10.00"
Money::JPY('10')->getDecimal(); // "10"
```

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

Run static analysis:

```
composer stan
```

Check code style:

```
composer cs
```

Fix code style:

```
composer cs-fix
```

Contributing
------------

[](#contributing)

Contributions are welcome.

Before opening a pull request, please ensure:

- tests pass: `composer test`
- code style passes: `composer cs`
- static analysis passes: `composer stan`

License
-------

[](#license)

MIT License © [Josef Traxler](https://github.com/joseftraxler)

See the [LICENSE](LICENSE) file for details.

Support
-------

[](#support)

- **Issues:** [GitHub Issues](https://github.com/joseftraxler/laravel-money/issues)
- **Source:** [GitHub Repository](https://github.com/joseftraxler/laravel-money)

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance94

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

19

Last Release

27d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b9d008573def85b94729be81c1c7f4216631d3b12c26ef9ecccf355a9306ae2e?d=identicon)[joseftraxler](/maintainers/joseftraxler)

---

Top Contributors

[![joseftraxler](https://avatars.githubusercontent.com/u/98177220?v=4)](https://github.com/joseftraxler "joseftraxler (110 commits)")

---

Tags

laravelmoneycurrencycastattribute

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/joseftraxler-laravel-money/health.svg)

```
[![Health](https://phpackages.com/badges/joseftraxler-laravel-money/health.svg)](https://phpackages.com/packages/joseftraxler-laravel-money)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.4k](/packages/larastan-larastan)[laravel/ai

The official AI SDK for Laravel.

9782.1M153](/packages/laravel-ai)[spatie/laravel-health

Monitor the health of a Laravel application

88011.3M149](/packages/spatie-laravel-health)[api-platform/laravel

API Platform support for Laravel

59156.3k10](/packages/api-platform-laravel)[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58122.8k](/packages/laracraft-tech-laravel-useful-additions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
