PHPackages                             ayup-creative/laravel-currency-cast - 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. ayup-creative/laravel-currency-cast

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

ayup-creative/laravel-currency-cast
===================================

Casts currency values to and from integers

v1.0.0(2mo ago)029PHPPHP &gt;=8.2CI passing

Since Apr 15Pushed 2mo agoCompare

[ Source](https://github.com/Ayup-Creative/laravel-currency-cast)[ Packagist](https://packagist.org/packages/ayup-creative/laravel-currency-cast)[ RSS](/packages/ayup-creative-laravel-currency-cast/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Laravel Currency Cast
=====================

[](#laravel-currency-cast)

A PHP library for handling monetary values with integer-based precision, including a Laravel custom caster for Eloquent models.

Features
--------

[](#features)

- Integer-based monetary calculations to avoid floating-point errors.
- Automatic casting of database values to `Money` objects in Laravel.
- Supports different currencies and formatting via `intl`.
- Arithmetic operations (add, subtract, multiply, divide, discount, sum).
- Comparison operations (greater than, less than, equals, etc.).
- Livewire support via `Wireable` interface.
- JSON serialization.

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

[](#installation)

```
composer require ayup-creative/laravel-currency-cast
```

Usage
-----

[](#usage)

### Money Value Object

[](#money-value-object)

You can create `Money` objects using the constructor or the `fromFloat` helper:

```
use AyupCreative\Casts\Currency\Values\Money;

// Directly from raw cents
$price = new Money(1000, 'GBP'); // £10.00

// From a float value
$price = Money::fromFloat(19.99, 'USD'); // $19.99

// Using the helper
$price = money(500); // £5.00 (defaults to GBP)
```

#### Arithmetic Operations

[](#arithmetic-operations)

```
$m1 = money(1000); // £10.00
$m2 = money(500);  // £5.00

$sum = $m1->add($m2);        // £15.00
$diff = $m1->subtract($m2);  // £5.00
$multi = $m1->multiply(1.5); // £15.00
$div = $m1->divide(2);       // £5.00
$discounted = $m1->discount(20); // £8.00 (20% off)
```

#### Comparisons

[](#comparisons)

```
$m1 = money(1000);
$m2 = money(2000);

$m1->isLessThan($m2); // true
$m1->isGreaterThan($m2); // false
$m1->isEqualTo(money(1000)); // true
$m1->isZero(); // false
$m1->isPositive(); // true
```

#### Formatting

[](#formatting)

```
$money = money(12345); // £123.45
echo $money->formatted('en_GB'); // "£123.45"
echo $money->formatted('en_US'); // "£123.45" (Currency code is respected)
echo (string) $money; // Uses default en_GB
```

### Laravel Caster

[](#laravel-caster)

To use the caster in your Eloquent models, add it to the `$casts` array:

```
use AyupCreative\Casts\Currency\Currency;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $casts = [
        'price' => Currency::class,
    ];

    // Optional: Define currency code for the model
    public $currencyCode = 'USD';

    // OR use a method
    public function getCurrencyCode()
    {
        return 'EUR';
    }
}
```

The caster can also resolve the currency code from another attribute on the model:

```
class Product extends Model
{
    protected $casts = [
        'price' => Currency::class,
    ];

    // Points to the 'currency' column in the database
    public $currencyCode = 'currency';
}
```

If neither `currencyCode` property nor `getCurrencyCode()` method exists, the caster will look for a configuration value: `config('currency.currency_code')`.

Testing
-------

[](#testing)

Run the test suite using PHPUnit:

```
composer test
```

Development
-----------

[](#development)

### Pre-commit Hooks

[](#pre-commit-hooks)

This repository uses [pre-commit](https://pre-commit.com/) to ensure code quality and commit message standards.

To install the hooks, run:

```
pre-commit install
```

The hooks include:

- `conventional-pre-commit`: Validates commit messages follow the conventional commit standard.
- `phpunit`: Runs the full test suite before each commit to ensure stability.

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance86

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.9% 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

Unknown

Total

1

Last Release

72d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/494de8188ad97403b72e40e385235a74ef6331aa2710b3727193f646189af9fc?d=identicon)[mykemeynell](/maintainers/mykemeynell)

---

Top Contributors

[![mykemeynell](https://avatars.githubusercontent.com/u/1590190?v=4)](https://github.com/mykemeynell "mykemeynell (31 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

castingcurrencylaravelphp

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ayup-creative-laravel-currency-cast/health.svg)

```
[![Health](https://phpackages.com/badges/ayup-creative-laravel-currency-cast/health.svg)](https://phpackages.com/packages/ayup-creative-laravel-currency-cast)
```

PHPackages © 2026

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