elegantly/laravel-money
Laravel integration for brick/money: Eloquent casting to Brick\Money\Money, safe parsing from strings/ints/floats, and a ValidMoney validation rule with min/max bounds and nullability. Store amounts cleanly (with currency column or fixed currency).
Strengths:
Brick/Money, which avoids floating-point errors via integer-based storage (smallest currency unit).Money objects, improving code clarity and reducing side-effect risks in financial operations.ValidMoney rule for input sanitization, critical for APIs and user-facing forms.Fit Gaps:
bigInteger + string (currency) columns for optimal precision, which may conflict with existing databases or legacy schemas.Brick/Money may need training to adopt the pattern.decimal/float columns would need migration to bigInteger + currency columns (backward-compatible parsing exists but sacrifices precision).MoneyCast.MoneyParser handles common formats (strings, ints, floats), easing integration with external systems.Money objects serialize to floats (e.g., 100.00) by default, which may require customization for APIs expecting strings (e.g., "USD 100.00").Brick/Money has 1.5K+ stars).decimal columns to bigInteger + currency requires downtime and testing.currency) to existing tables?Money objects be serialized for APIs (e.g., JSON)? Will clients expect strings like "USD 100.00" or floats like 100.00?bigInteger + string columns; SQLite may require adjustments for integer scaling.brick/money (v0.15.x), Laravel v10–12.ValidMoney) integrate with Livewire, Inertia.js, or vanilla Laravel forms.decimal/float columns, precision requirements).bigInteger (amount) + string (currency) from day one.amount_in_cents, currency) to tables.amount_in_cents from existing values (with rounding).Schema::table('orders', function (Blueprint $table) {
$table->bigInteger('amount_in_cents')->nullable()->after('amount');
$table->string('currency', 3)->nullable()->after('amount_in_cents');
});
MoneyCast:
protected $casts = [
'amount' => MoneyCast::of('currency'), // Dynamic currency
// OR
'price' => MoneyCast::of('USD'), // Fixed currency
];
MoneyParser::parse($model->amount * 100, $model->currency)).ValidMoney:
'price' => ['required', new ValidMoney(min: 0, max: 10000)],
Money serialization if needed (e.g., override toJson() or use accessors).public function getAmountStringAttribute(): string
{
return $this->amount->getAmount()->toString() . ' ' . $this->currency;
}
0.01 USD vs. 0.02 USD calculations).Brick/Money dependencies).Money objects in exports.Invoice, Subscription, Order).MoneyCast and validation.ValidMoney.amount_in_cents for existing records.ValidMoney rule reduces duplicate validation logic.Brick/Money and Laravel for breaking changes (e.g., v2.0.0’s namespace shift).Brick/Money’s API (e.g., Money::add(), Money::getAmount()).How can I help you explore Laravel packages today?