Skip to main content
The package provides convenient request macros that allow you to retrieve Money and Currency instances directly from incoming HTTP requests.

Available Macros

Request::currency()

Extract a Currency instance from the request input.
use Illuminate\Support\Facades\Request;

$currency = request()->currency('currency');
// Returns: Devhammed\LaravelBrickMoney\Currency instance or null
Signature
key
string
required
The input field name to extract the currency from
default
string | null
default:"null"
The default currency code to use if the input is not present
Returns Currency|null - Returns a Currency instance if a valid currency code is found, otherwise null

Request::money()

Extract a Money instance from the request input.
use Illuminate\Support\Facades\Request;

$price = request()->money('price');
// Returns: Devhammed\LaravelBrickMoney\Money instance or null
Signature
key
string
required
The input field name to extract the money amount from
default
BigNumber|string|int|float|null
default:"null"
The default value to use if the input is not present
currency
string | null
default:"null"
The currency code to use for the money instance. If not provided, uses the default currency from config
minor
bool | null
default:"null"
Whether the amount is in minor units (cents). If true, treats the input as minor units
context
Context | null
default:"null"
The Brick\Money\Context instance to use for the money object
roundingMode
RoundingMode
default:"RoundingMode::UNNECESSARY"
The rounding mode to use when converting the amount
Returns Money|null - Returns a Money instance if valid input is found, otherwise null

Usage Examples

Basic Usage

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function store(Request $request)
    {
        $price = $request->money('price');
        $currency = $request->currency('currency');
        
        // $price is a Money instance
        // $currency is a Currency instance
        
        return response()->json([
            'price' => $price->format(),
            'currency' => $currency->getCode(),
        ]);
    }
}

With Currency Parameter

You can specify the currency directly when extracting money from the request:
$price = request()->money('price', currency: 'EUR');
// Converts the input to EUR Money instance

$price = request()->money('price', currency: 'USD');
// Converts the input to USD Money instance

Minor Units (Cents)

Handle amounts in minor units by setting the minor parameter:
// If input is 1000 cents
$price = request()->money('price_cents', currency: 'EUR', minor: true);
// Returns: €10.00 (converts 1000 cents to 10.00 EUR)
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:
$currency = request()->currency('currency', 'USD');
// Falls back to USD if 'currency' input is not provided

$price = request()->money('price', default: 0, currency: 'USD');
// Falls back to $0.00 if 'price' input is not provided

Combined Example

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateProductRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'price' => 'required|numeric',
            'currency' => 'required|string',
        ];
    }
    
    public function getPrice()
    {
        return $this->money('price', currency: $this->input('currency'));
    }
    
    public function getCurrency()
    {
        return $this->currency('currency');
    }
}

Implementation Details

The macros are registered in the package’s service provider:
Provider.php
Request::macro('currency', function (string $key, ?string $default = null): ?Currency {
    $value = $this->input($key, $default);

    if (! is_string($value)) {
        return null;
    }

    return currency($value);
});

Request::macro('money', function (
    string $key,
    BigNumber|string|int|float|null $default = null,
    ?string $currency = null,
    ?bool $minor = null,
    ?Context $context = null,
    RoundingMode $roundingMode = RoundingMode::UNNECESSARY
): ?Money {
    $value = $this->input($key, $default);

    if (! is_string($value) && ! is_int($value) && ! is_float($value)) {
        return null;
    }

    return money($value, $currency, $minor, $context, $roundingMode);
});

Type Safety

Both macros return null 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

Build docs developers (and LLMs) love