Available Cast Types
The package provides three cast types for different use cases:AsIntegerMoney
Store money in minor units (cents) - Recommended
AsDecimalMoney
Store money in major units (dollars)
AsCurrency
Cast currency codes to Currency objects
Storage Strategies
Each money cast supports two storage strategies:Separate Columns (Recommended)
Store the amount and currency in separate database columns. This approach offers better queryability and indexing capabilities.The first argument to
of() specifies the currency column name. The amount is stored in the cast attribute itself (price in this example).JSON Column
Store both amount and currency in a single JSON column. This is useful when you want to keep monetary values self-contained.Quick Comparison
| Cast Type | Storage Format | Use Case | Precision |
|---|---|---|---|
| AsIntegerMoney | Minor units (cents) | General purpose, cryptocurrencies | Exact |
| AsDecimalMoney | Major units (dollars) | Human-readable storage | Decimal |
| AsCurrency | Currency code | Currency reference only | N/A |
Basic Usage Example
Key Benefits
Type Safety
Type Safety
Work with strongly-typed Money objects throughout your application instead of raw numbers.
Automatic Conversion
Automatic Conversion
The casts handle all serialization and deserialization between your database and PHP objects.
Currency Awareness
Currency Awareness
Each Money object knows its currency, preventing accidental mixing of different currencies.
Precision Handling
Precision Handling
Avoid floating-point arithmetic issues with proper money handling using the Brick\Money library.
Choosing the Right Cast
Use AsIntegerMoney when:
- Working with standard currencies (USD, EUR, GBP, etc.)
- Handling cryptocurrencies with high precision
- You need exact arithmetic without rounding errors
- Building financial applications (recommended)
Use AsDecimalMoney when:
- You need human-readable values in the database
- Integrating with legacy systems that store decimals
- Working with reporting tools that expect decimal values
Use AsCurrency when:
- You only need to store and retrieve currency information
- Working alongside money casts for the currency column
- Building multi-currency product catalogs
Next Steps
AsIntegerMoney Cast
Learn about the recommended cast for storing money
AsDecimalMoney Cast
Explore decimal-based money storage