yuwuhsien/laravel-bcmath-cast
Eloquent casts for PHP 8.4's BCMath\Number object API. Turn model attributes into arbitrary-precision decimals and do natural operator-based arithmetic—add, subtract, multiply, divide—without bcadd/bcmul boilerplate. Requires PHP 8.4+ and Laravel 12+.
$invoice->subtotal - $invoice->discount directly on model attributes with arbitrary precision, which shortens the path from spec to shippable code.BCMath\Number. Building this internally is a day of work, so the real decision is not "buy vs. build" — it is "buy vs. build vs. do nothing." Choosing this package is a cheap decision; the expensive decisions it forces (schema audit, rounding policy, API serialization contract) are the ones worth the roadmap discussion.FLOAT/DOUBLE columns holding currency or rates. Even if you defer the package, that audit is a legitimate roadmap item with its own risk-reduction value — float money columns are a correctness bug waiting for a reconciliation to expose it.Consider it when all of the following hold:
DECIMAL with explicit precision and scale. On FLOAT/DOUBLE columns the cast delivers zero correctness benefit — the schema work is the actual value, the cast is the ergonomics.string to BCMath\Number inside every reader of that attribute.Look elsewhere or defer when:
To executives — one paragraph: We have a small, low-cost opportunity to eliminate a class of money-rounding errors that typically only shows up in a finance reconciliation. PHP 8.4 and Laravel 12 introduced native arbitrary-precision decimal objects; this MIT-licensed package connects them to our models so engineers can write ordinary arithmetic on prices and totals without losing cents. The package itself is trivial to install and trivial to remove — the real work, and the real value, is the associated audit of float-typed money columns and a written rounding policy, both of which we should do regardless of whether we adopt this. The risk is vendor immaturity, not technical fragility: it is a two-star, single-maintainer project. We mitigate that by depending on PHP's built-in BCMath\Number rather than on any vendor-owned class, so if the project disappears we rewrite one cast class and keep our data. Recommendation: pilot on one low-risk model with an acceptance test suite, gated on confirming our PHP/Laravel versions.
To engineering — the ask:
FLOAT/DOUBLE → DECIMAL schema audit as a prerequisite deliverable with its own owner and estimate, not a follow-on task.Number, Number→round-trip, null, 0, empty string, values exceeding column precision failing loudly rather than truncating, and scale assertions. Round-trip fidelity has already been a defect class upstream (v1.2.2: "align set() input validation with what get() can parse back"), so we write our own tests rather than trusting theirs.SUM() over DECIMAL agrees with PHP-side reduction over Number objects on representative data. Divergence surfaces in reconciliation, not in CI.BCMath\Number in PHP, or any unresolvable round-trip defect.AsDecimal; counters, IDs, sequence numbers, and analytical aggregates stay native. Without this rule the cast spreads into fields where it adds cost without correctness.How can I help you explore Laravel packages today?