- How do I install brick/math in a Laravel 10+ project?
- Run `composer require brick/math` in your project root. The package requires PHP 8.2+ and works with Laravel 10+. For older PHP versions (7.1–8.1), install a compatible version (e.g., `0.13` for PHP 8.1) via Composer. No Laravel-specific setup is needed beyond requiring the package.
- Can brick/math replace PHP’s float for financial calculations in Laravel?
- Yes. Use `BigDecimal` instead of `float` to avoid rounding errors (e.g., `0.1 + 0.2`). Initialize with `BigDecimal::of('1.23')` and chain methods like `plus()`, `minus()`, or `dividedBy()`. Store results as strings in the database (e.g., `TEXT` columns) for precision.
- What happens if GMP or BCMath extensions are missing in production?
- The library falls back to pure PHP calculations, but performance degrades significantly. Test your app’s critical paths in a Docker container with `php:8.2-cli-alpine` (no extensions) to verify fallback behavior. For production, ensure GMP/BCMath is installed—it’s auto-selected at runtime.
- How do I integrate brick/math with Laravel’s service container?
- Bind the classes to interfaces in `AppServiceProvider`. Example: Register `BigInteger`, `BigDecimal`, and `BigRational` as a `MathService` and inject it into controllers/services. This enables mocking in tests and centralizes arithmetic logic (e.g., `OrderService::calculateTotal()`).
- Are there breaking changes when upgrading brick/math versions?
- Yes, breaking changes start a new `0.x` cycle (e.g., `0.17` → `0.18`). Lock to `^0.17` in `composer.json` to avoid surprises. Check the [release history](https://github.com/brick/math/releases) for changes. Use Laravel’s deprecation channel to log warnings for deprecated methods.
- How do I test brick/math in Laravel unit tests with PHPUnit?
- Mock GMP/BCMath extensions using `phpunit/phpunit-mock-objects`. Test edge cases like overflow (`IntegerOverflowException`), rounding (`RoundingNecessaryException`), and serialization round-trips. Example: Serialize `BigDecimal` to JSON and assert equality after deserialization.
- What’s the best way to store BigDecimal values in Laravel’s database?
- Store as strings in `TEXT` columns (e.g., `BigDecimal::toString()`). Avoid floating-point columns like `DECIMAL` to preserve precision. For Eloquent models, cast attributes to `BigDecimal` using accessors/mutators. Example: `return BigDecimal::of($this->amount);`.
- Does brick/math work with Laravel queues (e.g., Redis) or caching?
- Yes, but serialize to strings first. Use `BigDecimal::toString()` for Redis or JSON cache storage. Deserialize with `BigDecimal::of($string)`. Avoid storing raw objects directly to prevent extension dependency conflicts in shared environments.
- How do I handle floating-point inputs safely (e.g., from APIs)?
- Use `BigDecimal::fromFloatExact()` for exact conversion or `fromFloatShortest()` to round to the nearest representable value. This prevents silent precision loss (e.g., `0.1 + 0.2` becoming `0.30000000000000004`). Validate inputs in Laravel Form Requests with custom error messages.
- Are there alternatives to brick/math for arbitrary-precision math in Laravel?
- Other options include `php-gmp` (lower-level) or `bcmath` (built into PHP), but neither offers an OOP API or immutability. For Laravel, `brick/math` stands out for its clean API, GMP/BCMath acceleration, and seamless integration with Laravel’s patterns (e.g., value objects, DI).