- How do I install spatie/weight-conversions in a Laravel project?
- Run `composer require spatie/weight-conversions` in your project root. The package has no Laravel-specific dependencies, so it works immediately in any PHP 8.0+ environment, including Laravel 8+. No additional configuration or service provider setup is required.
- Can I use this package for API request/response transformations in Laravel?
- Yes, it’s perfect for APIs. Use the `Weight` class to convert units dynamically in controllers or DTOs. For example, accept pounds in requests but store kilograms in your database, then convert back when returning responses. The package is stateless and thread-safe for queued jobs.
- Does this package support custom or non-standard weight units (e.g., metric tons, carats)?
- No, the package only supports standard units (grams, kilograms, pounds, ounces, etc.). If you need custom units, you’ll need to fork the repository or create a wrapper class to extend its functionality, as the codebase is simple enough to modify for your needs.
- Is spatie/weight-conversions compatible with Laravel 10+? Will it break in future Laravel versions?
- The package has no Laravel-specific code, so it will work with any Laravel 8+ version. However, since the last release was in 2021, test it in your Laravel version’s PHP environment (8.0+) to confirm compatibility. For critical projects, vendor the package to avoid dependency risks.
- How accurate are the conversions, and does it handle edge cases like negative weights or scientific notation?
- The package uses precise PHP calculations for conversions, but edge cases like negative weights or extremely large values (e.g., scientific notation) aren’t explicitly validated. Test these scenarios in your application logic, as the package assumes valid input by default.
- Should I use this package in database migrations or Eloquent models for weight storage?
- No, avoid storing converted weights in migrations or models. Instead, store raw values (e.g., kilograms) and use accessors or service methods to convert for display or API responses. This keeps your database schema clean and avoids recalculating conversions unnecessarily.
- Is there a performance impact when using this package in high-traffic Laravel applications?
- The package is lightweight, with negligible overhead since it performs pure PHP calculations. It won’t impact scalability, even in high-traffic APIs or background jobs. However, if you’re processing millions of conversions per second, consider caching frequent results.
- What are the alternatives to spatie/weight-conversions for Laravel projects?
- For simple needs, a static utility class or Laravel Helper might suffice. For more advanced use cases, consider libraries like `phpunit/phpunit` (for testing) or `symfony/options-resolver` (for dynamic unit handling). However, no other Laravel-specific weight conversion packages exist, making this the most straightforward choice.
- How can I test this package in my Laravel application to ensure reliability?
- Write unit tests for critical conversions using PHPUnit. Test edge cases like zero, negative values, and large numbers. Since the package is standalone, you can also test it in isolation before integrating it into your Laravel services or controllers.
- The package hasn’t been updated since 2021. Should I still use it, or is there a risk of breaking changes?
- The package is stable and has no Laravel dependencies, so breaking changes are unlikely. However, for production-critical projects, vendor the package (`composer vendor:publish`) to lock the version. Monitor for forks or consider wrapping the core logic to isolate it from future updates.