Money and Currency instances directly from incoming HTTP requests.
Available Macros
Request::currency()
Extract aCurrency instance from the request input.
The input field name to extract the currency from
The default currency code to use if the input is not present
Currency|null - Returns a Currency instance if a valid currency code is found, otherwise null
Request::money()
Extract aMoney instance from the request input.
The input field name to extract the money amount from
The default value to use if the input is not present
The currency code to use for the money instance. If not provided, uses the default currency from config
Whether the amount is in minor units (cents). If
true, treats the input as minor unitsThe
Brick\Money\Context instance to use for the money objectThe rounding mode to use when converting the amount
Money|null - Returns a Money instance if valid input is found, otherwise null
Usage Examples
Basic Usage
With Currency Parameter
You can specify the currency directly when extracting money from the request:Minor Units (Cents)
Handle amounts in minor units by setting theminor parameter:
When
minor: true, the input is treated as the smallest unit of the currency (e.g., cents for USD, pence for GBP).With Default Values
Provide default values when inputs might be missing:Combined Example
Implementation Details
The macros are registered in the package’s service provider:Provider.php
Type Safety
Both macros returnnull when:
- The input is not present and no default is provided
- The input value is of an invalid type
- For
currency(): the value cannot be converted to a string - For
money(): the value cannot be converted to a numeric type
Always check for
null returns when using these macros without validation, or use them in conjunction with validation rules to ensure valid input.Next Steps
Validation Rules
Validate money and currency inputs
JSON Serialization
Customize JSON output format