PHPackages                             fezz/money-magic - 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. [Database &amp; ORM](/categories/database)
4. /
5. fezz/money-magic

ActiveLibrary[Database &amp; ORM](/categories/database)

fezz/money-magic
================

Generate consistent Brick\\Money accessors for Eloquent "money" fields stored as minor units (ints). Define money mappings once, avoid floats, keep currency handling centralized, and optionally scaffold mappings via Artisan for clean, type-safe monetary domain code.

v0.1.1(3mo ago)0189↑109.3%[1 PRs](https://github.com/fezz02/money-magic/pulls)MITPHPPHP ^8.3CI passing

Since Jan 25Pushed 1mo agoCompare

[ Source](https://github.com/fezz02/money-magic)[ Packagist](https://packagist.org/packages/fezz/money-magic)[ Docs](https://github.com/fezz/money-magic)[ GitHub Sponsors](https://github.com/fezz)[ RSS](/packages/fezz-money-magic/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (17)Versions (4)Used By (0)

Money Magic
===========

[](#money-magic)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b249a19527db932e5e16fcfd0ca94cf03690e67e08977aeab45ff2516d8d0628/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f66657a7a2f6d6f6e65792d6d616769632e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fezz/money-magic)[![Total Downloads](https://camo.githubusercontent.com/4c0089f6b4ee94b4de048e1cf8ac0987bddf579bacb08debec29ede8d0b5838a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f66657a7a2f6d6f6e65792d6d616769632e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fezz/money-magic)[![License](https://camo.githubusercontent.com/997699ea2910994a62be59494b7a2c1a4477b14c54ec2d6f824366b47b49af84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f66657a7a2f6d6f6e65792d6d616769632e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fezz/money-magic)[![GitHub Tests Action Status](https://camo.githubusercontent.com/ee83faec0f4fdacafa95affbde78ec866c206e8464996af350530da94dcbad19/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f66657a7a30322f6d6f6e65792d6d616769632f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/fezz02/money-magic/actions?query=workflow%3Arun-tests+branch%3Amain)

Generate consistent `Brick\Money` accessors for Eloquent "money" fields stored safely as **INTEGER minor units** (e.g., cents). Define money mappings once, avoid floats, keep currency handling centralized, and get clean, type-safe monetary domain code.

Why Money Magic?
----------------

[](#why-money-magic)

Storing money as floats in databases is a common mistake that leads to precision errors. Money Magic solves this by:

- **Storing money as integers** (minor units like cents) in your database
- **Never storing floats** - all float accessors are derived from the integer storage
- **Providing type-safe accessors** using `Brick\Money` for calculations
- **Auto-hiding internal fields** in JSON output (configurable)
- **Centralized currency handling** through a simple configuration

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

[](#requirements)

- PHP ^8.3
- Laravel ^11.0 or ^12.0

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

[](#installation)

You can install the package via composer:

```
composer require fezz/money-magic
```

Publishing Configuration
------------------------

[](#publishing-configuration)

You can publish the config file with:

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

This will publish the configuration file to `config/money-magic.php` where you can customize suffixes, auto-hide behavior, and enabled toggles.

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

Use the `HasMoneyAttributes` trait in your model and define a `$money` mapping:

```
use Fezz\MoneyMagic\Concerns\HasMoneyAttributes;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasMoneyAttributes;

    protected array $money = [
        'price' => 'currency', // or null to use default currency-column
    ];
}
```

### Available Attributes

[](#available-attributes)

Based on the default configuration, the following attributes are automatically available:

- `$product->price` - Float accessor (major units) if float is enabled and suffix is empty
- `$product->price_minor` - Integer accessor (minor units, e.g., cents)
- `$product->price_money` - `Brick\Money\Money` instance (nullable)
- `$product->price_formatted` - Formatted string accessor (read-only)

### Auto-Hide Behavior

[](#auto-hide-behavior)

By default, internal fields (`price_minor` and `price_money`) are automatically hidden from JSON output. This behavior is configurable via the `autohide` configuration.

### Example Usage

[](#example-usage)

```
// Setting a price (stores as minor units)
$product->price = 12.99; // Stores 1299 in price_minor column

// Accessing the price
$product->price; // 12.99 (float)
$product->price_minor; // 1299 (int)
$product->price_money; // Brick\Money\Money instance
$product->price_formatted; // "€12,99" (formatted string)

// Working with Money object
$product->price_money->multiply(2); // Returns new Money instance
$product->price_money->getAmount(); // Decimal instance
```

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

[](#configuration)

The configuration file (`config/money-magic.php`) allows you to customize:

- **`currency-column`** - The database column name that stores the ISO currency code (default: `'currency'`)
- **`float.enabled`** - Enable/disable the float accessor (default: `true`)
- **`float.suffix`** - Suffix for float accessor (empty string uses base field name, default: `''`)
- **`minor.suffix`** - Suffix for minor units column (default: `'_minor'`)
- **`money.enabled`** - Enable/disable the Money accessor (default: `true`)
- **`money.suffix`** - Suffix for Money accessor (default: `'_money'`)
- **`formatted.enabled`** - Enable/disable the formatted accessor (default: `true`)
- **`formatted.suffix`** - Suffix for formatted accessor (default: `'_formatted'`)
- **`formatted.format`** - Locale for formatting (default: `'it-IT'`)
- **`autohide`** - Array controlling which fields are hidden in JSON output:
    - `minor` (default: `true`)
    - `money` (default: `true`)
    - `formatted` (default: `false`)
    - `float` (default: `false`)

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](SECURITY.md) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [fezz02](https://github.com/fezz02)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 88% of packages

Maintenance92

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.7% 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 ~0 days

Total

2

Last Release

103d ago

### Community

Maintainers

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

---

Top Contributors

[![fezz02](https://avatars.githubusercontent.com/u/95986772?v=4)](https://github.com/fezz02 "fezz02 (18 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

laravelmoneycurrencyValue Objecteloquentaccessorscastsbrick-money

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/fezz-money-magic/health.svg)

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

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17649.9k](/packages/lacodix-laravel-model-filter)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[indexzer0/eloquent-filtering

Powerful eloquent filtering

22425.9k3](/packages/indexzer0-eloquent-filtering)[giacomomasseron/laravel-models-generator

Generate Laravel models from an existing database

484.2k](/packages/giacomomasseron-laravel-models-generator)

PHPackages © 2026

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