price is cast with money-for-laravel’s AsMoney::currencyColumn('currency') to a RoundlyConsulting\Money\Money: the amount lives in price as minor units (a decimal(38,0) column), the code in currency.
use RoundlyConsulting\Money\Money;
$ad->price = Money::ofMajor('15.00', 'EUR');
$ad->price->minor(); // "1500" — a string, never (int)-cast it
$ad->price->toDecimal(); // "15.00"
$ad->price->format('sk'); // formatted for the Slovak locale
$ad->currency; // "EUR"
$ad->price?->add(Money::ofMinor(500, 'EUR')); // money's API; mismatched currencies throw CurrencyMismatch
Advertisement::query()->orderBy('price')->get(); // numeric orderRules
- Only Money|null can be assigned; anything else throws RoundlyConsulting\Money\Exceptions\InvalidMoneyValue.
- Assigning a price in another currency to a priced ad throws CurrencyMismatch — set currency first to re-denominate on purpose (UpdateAdvertisement does this for you).
- Writing null clears the amount and keeps currency.
- PostgreSQL and MySQL store all 38 digits; SQLite is exact only within int64, so the cast refuses wider amounts there (InvalidMoneyValue) instead of silently storing a float.
- The text-ad fallback renders $ad->price->format() in the active locale.
API resources
Render the price with money’s resource — from() returns null for a price-less ad:
use RoundlyConsulting\Money\Http\Resources\MoneyResource;
'price' => MoneyResource::from($ad->price),
// {"minor":"1999","decimal":"19.99","currency":"EUR","exponent":2,"formatted":"…"}
// "formatted" follows the active app locale: app()->setLocale('sk') changes itminor is a JSON string on purpose; a frontend doing arithmetic reads decimal (or BigInt(minor)). formatted follows the active app locale (app()->setLocale()), so it changes with the request’s language — display it, never parse it. Arithmetic, formatting, currencies and rounding are covered in the Money for Laravel docs.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.