Laravel Brick Money provides custom Livewire synthesizers that enable you to use Money and Currency objects as public properties in your Livewire components. The synthesizers handle serialization and deserialization automatically.
You can use Money objects as public properties in your Livewire components:
use Devhammed\LaravelBrickMoney\Money;use Livewire\Component;class ProductPrice extends Component{ public Money $price; public function mount() { $this->price = Money::of(99.99, 'USD'); } public function increasePrice() { $this->price = $this->price->plus(10); } public function render() { return view('livewire.product-price'); }}
Product Price Component
<div> <h2>Current Price: {{ $price->format() }}</h2> <button wire:click="increasePrice">Increase by $10</button></div>
You can bind to specific properties of Money objects:
use Devhammed\LaravelBrickMoney\Money;use Devhammed\LaravelBrickMoney\Currency;use Livewire\Component;class PriceEditor extends Component{ public Money $price; public function mount() { $this->price = Money::of(0, 'USD'); } public function render() { return view('livewire.price-editor'); }}
use Devhammed\LaravelBrickMoney\Currency;use Livewire\Component;class CurrencySelector extends Component{ public Currency $currency; public function mount() { $this->currency = Currency::of('USD'); } public function changeCurrency(string $code) { $this->currency = Currency::of($code); } public function render() { return view('livewire.currency-selector'); }}
The Money synthesizer supports all Brick Money context types:
Default Context
Custom Context
Cash Context
Auto Context
public Money $price;public function mount(){ $this->price = Money::of(100, 'USD'); // Uses DefaultContext automatically}
use Brick\Money\Context\CustomContext;public Money $price;public function mount(){ $context = new CustomContext(4); $this->price = Money::of(100, 'USD', $context);}
use Brick\Money\Context\CashContext;public Money $price;public function mount(){ $context = new CashContext(5); // Round to nearest 5 cents $this->price = Money::of(100, 'USD', $context);}
use Brick\Money\Context\AutoContext;public Money $price;public function mount(){ $context = new AutoContext(); $this->price = Money::of(100, 'USD', $context);}
use Devhammed\LaravelBrickMoney\Money;use Devhammed\LaravelBrickMoney\Currency;use Livewire\Component;class PriceCalculator extends Component{ public Money $basePrice; public float $markup = 0; public float $discount = 0; public Currency $currency; public function mount() { $this->currency = Currency::of('USD'); $this->basePrice = Money::of(100, $this->currency); } public function updatedCurrency($value) { $this->currency = Currency::of($value); $this->basePrice = Money::of( $this->basePrice->getAmount(), $this->currency ); } public function getFinalPriceProperty(): Money { $price = $this->basePrice; // Apply markup if ($this->markup > 0) { $markupAmount = $price->multipliedBy($this->markup / 100); $price = $price->plus($markupAmount); } // Apply discount if ($this->discount > 0) { $discountAmount = $price->multipliedBy($this->discount / 100); $price = $price->minus($discountAmount); } return $price; } public function render() { return view('livewire.price-calculator'); }}
Computed properties like getFinalPriceProperty() work seamlessly with Money objects. Just return a Money instance and Livewire will handle serialization automatically.
You can validate Money and Currency properties using Livewire’s validation:
use Devhammed\LaravelBrickMoney\Rules\MoneyRule;use Devhammed\LaravelBrickMoney\Rules\CurrencyRule;class ProductForm extends Component{ public Money $price; public Currency $currency; public function rules() { return [ 'price.amount' => ['required', new MoneyRule(min: 0, max: 10000)], 'currency.code' => ['required', new CurrencyRule()], ]; } public function save() { $this->validate(); // Save product with validated price and currency }}
The synthesizers handle property access automatically, so $this->price->amount works in wire:model bindings and validation rules.