PHPackages                             origami/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. origami/money

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

origami/money
=============

Money handling and formatting package for Laravel projects

2.2.0(3mo ago)17.8kMITPHPPHP ^8.2

Since Mar 30Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/papertank/origami-money)[ Packagist](https://packagist.org/packages/origami/money)[ RSS](/packages/origami-money/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (11)Used By (0)

Origami Money for Laravel
=========================

[](#origami-money-for-laravel)

This package is a money helper for Laravel projects and a wrapper around [moneyphp/money](https://github.com/moneyphp/money).

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

[](#installation)

Install this package through Composer.

```
composer require origami/money

```

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

[](#requirements)

- This package is designed to work with Laravel &gt;= 10.
- The `ext-intl` PHP extension is required.

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

[](#configuration)

First, publish the default configuration.

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

This will add a new configuration file to: `config/money.php` which contains a `default_currency` value.

Usage
-----

[](#usage)

### Value Object

[](#value-object)

The `Origami\Money\Money` value object class is a helper that uses a `Money\Money` object (from [moneyphp/money](https://github.com/moneyphp/money)) under the hood. This allows us to add some helper methods (e.g. for formatting) and be more opinionated about our implementation. The class does not extend `Money\Money` since that is a `final class` - instead we use an internal attribute.

As with `Money\Money`, each `Origami\Money\Money` object requires an:

- `$amount`, expressed in the smallest units of $currency (eg cents)
- `$currency`, an ISO-4217 3 character code for currency.

For example:

```
$money = new Origami\Money\Money(100, 'GBP') // £1
$money = new Origami\Money\Money(50000, 'USD') // $500
$money = Origami\Money\Money::make(1000000, 'USD') // $10,000
```

You can also pass a moneyphp object:

```
$currency = new Money\Currency('GBP');
$base = new Money\Money(100, $currency);
$money = new Origami\Money\Money($base, $currency);
```

### Available Methods

[](#available-methods)

#### instance

[](#instance)

Create an instance from `Money\Money` object:

```
Origami\Money\Money::instance(new Money\Money(500, new Money\Currency('GBP')));
```

#### copy

[](#copy)

Create a copy of the instance

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$another = $money->copy();
$andAnother = $money->clone(); // Alias for above
```

#### asBase

[](#asbase)

Get the underlying `Money\Money` object

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$base = $money->asBase(); // Money\Money
```

#### isSameCurrency

[](#issamecurrency)

```
$gbp = new Origami\Money\Money(500, 'GBP'); // £5
$usd = new Origami\Money\Money(500, 'USD'); // $5
$money->isSameCurrency(); // false
```

#### equals

[](#equals)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = Origami\Money\Money::make(500, 'GBP'); // £5
$money->equals(); // true
```

#### greaterThan / gt

[](#greaterthan--gt)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(600, 'GBP'); // £6
$second->greaterThan($first); // true
$second->gt($first); // Alias for above
```

#### greaterThanOrEqual / gte

[](#greaterthanorequal--gte)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(500, 'GBP'); // £5
$second->greaterThanOrEqual($first); // true
$second->gte($first); // Alias for above
```

#### lessThan / lt

[](#lessthan--lt)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(600, 'GBP'); // £6
$second->lessThan($first); // false
$second->lt($first); // Alias for above
```

#### lessThanOrEqual / lte

[](#lessthanorequal--lte)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(500, 'GBP'); // £5
$second->lessThanOrEqual($first); // true
$second->lte($first); // Alias for above
```

#### isZero

[](#iszero)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$zero = new Origami\Money\Money(0, 'GBP'); // £0
$first->isZero(); // false
$zero->isZero(); // true
```

#### isPositive

[](#ispositive)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$first->isPositive(); // true
```

#### isNegative

[](#isnegative)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$first->isNegative(); // false
```

#### percentageOf

[](#percentageof)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(1000, 'GBP'); // £10
$first->percentageOf($second); // 50
```

Set `$overflow` argument to `false` to not go over 100

```
$first = new Origami\Money\Money(1000, 'GBP'); // £10
$second = new Origami\Money\Money(500, 'GBP'); // £5
$first->percentageOf($second, false); // 100
```

#### getAmount

[](#getamount)

Returns `integer`

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$amount = $money->getAmount(); // 500
```

#### getCurrency

[](#getcurrency)

Returns `Money\Money` currency value object

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$currency = $money->getCurrency(); // `Money\Currency('GBP')`
```

#### getCurrencyCode

[](#getcurrencycode)

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$currency = $money->getCurrencyCode(); // "GBP"
```

#### add

[](#add)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(400, 'GBP'); // £4
$first->add($second); // `Origami\Money(900, 'GBP')`
```

#### subtract

[](#subtract)

```
$first = new Origami\Money\Money(500, 'GBP'); // £5
$second = new Origami\Money\Money(400, 'GBP'); // £4
$first->add($second); // `Origami\Money(100, 'GBP')`
```

#### multiply

[](#multiply)

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$money->multiply(2); // `Origami\Money(1000, 'GBP')`
```

#### divide

[](#divide)

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$money->divide(2); // `Origami\Money(250, 'GBP')`
```

#### mod

[](#mod)

Modulus operation

```
$money = new Origami\Money\Money(830, 'GBP'); // £8.30
$divisor = new Origami\Money\Money(300, 'GBP'); // £3.00
$money->add($divisor); // `Origami\Money(230, 'GBP')`
```

#### ratioOf

[](#ratioof)

Provides the ratio of a Money object compared to another

```
$three = new Origami\Money\Money(300, 'GBP'); // £3
$six = new Origami\Money\Money(600, 'GBP'); // £6
$three->ratioOf($six); // 0.5
```

#### absolute

[](#absolute)

```
$money = new Origami\Money\Money(-500, 'GBP'); // -£5
$money->absolute(); // `Origami\Money(500, 'GBP')`
```

#### negative

[](#negative)

```
$money = new Origami\Money\Money(500, 'GBP'); // £5
$money->negative(); // `Origami\Money(-500, 'GBP')`
```

Formatting
----------

[](#formatting)

### String

[](#string)

Uses Intl formatter for currency symbol and separatator.

```
$money = new Origami\Money\Money(500, 'GBP');
$money->format(); // Outputs: £5.00
(string) $money; // Outputs: £5.00 (uses `__toString()`)
app('origami-money.formatter')->format($money) // Outputs: £5.00
```

### String Neat (without Trailing Zeros)

[](#string-neat-without-trailing-zeros)

Only drops trailing zeros were decimal is zero.

```
$money = new Origami\Money\Money(500, 'GBP');
$money->formatNeat(); // Outputs: £5
app('origami-money.formatter')->formatNeat($money) // Outputs: £5
```

### Decimal

[](#decimal)

```
$money = new Origami\Money\Money(500, 'GBP');
$money->formatDecimal(); // Outputs: 5.00
$money->toDecimal(); // Alias for above
app('origami-money.formatter')->formatDecimal($money) // Outputs: 5.00
```

Blade Directives
----------------

[](#blade-directives)

You can use the following directives in your Blade views:

```
@money($money)
// same as `$money->format()`
```

```
@moneyNeat($money)
// same as `$money->formatNeat()`
```

Eloquent Attribute Cast
-----------------------

[](#eloquent-attribute-cast)

You are most likely storing your money values in your Eloquent models. This package provides an `Origami\Money\Casts\Money` [custom Laravel cast](https://laravel.com/docs/10.x/eloquent-mutators#custom-casts) for you to use:

```
