- How do I install frost/distance in a Laravel project?
- Use Composer to install the package with `composer require frost/distance`. No additional Laravel-specific setup is required, though you may want to bind it to the service container for dependency injection. The package has no dependencies beyond PHP core, so installation is straightforward.
- Does frost/distance support Laravel’s service container?
- Yes, you can register the Distance class as a singleton in your Laravel service provider for easier dependency injection. This is especially useful if you plan to use it across multiple parts of your application, like in form requests, models, or API responses.
- What Laravel versions does frost/distance work with?
- The package is compatible with Laravel 9 and 10, as it requires PHP 8.0+. While it lacks explicit PHP version constraints, the codebase suggests compatibility back to PHP 7.4, so it should work seamlessly in modern Laravel applications.
- Can I use frost/distance for user-facing distance conversions in forms?
- Yes, it’s ideal for form handling. You can validate and convert user inputs (e.g., miles to kilometers) using Laravel’s FormRequest validation combined with the Distance class. For example, validate the input first, then convert it before storing or processing.
- How do I handle invalid distance units like 'lightyears'?
- The package throws exceptions for invalid units. You can catch these exceptions in your application logic or use Laravel’s exception handling to return user-friendly error messages. For example, wrap the conversion in a try-catch block or validate units before conversion.
- Is frost/distance suitable for high-performance applications or batch processing?
- Yes, the package is lightweight and stateless, making it efficient for high-throughput scenarios like batch processing. However, if you’re converting thousands of distances in a loop, consider benchmarking to ensure it meets your performance requirements.
- Can I use frost/distance to standardize distance units across my Laravel API responses?
- Absolutely. You can use the package in API Resources or Transformers to ensure consistent units (e.g., always return meters for internal systems). This simplifies client-side logic and ensures uniformity across your API.
- What’s the best way to migrate legacy data stored in mixed distance units?
- Create a Laravel migration or Artisan command to iterate through your database records, convert each distance using frost/distance, and update the values. For example, you could loop through a model’s query builder results and apply conversions before saving.
- Are there alternatives to frost/distance for distance conversions in Laravel?
- You could use simple arithmetic (e.g., `$distance * 0.621371` for km to miles), but frost/distance provides a cleaner, more maintainable API and handles edge cases like invalid units. For geospatial calculations, consider pairing it with libraries like geocoder/geocoder.
- How do I test frost/distance in a Laravel application?
- Since the package is stateless, testing is straightforward. Mock the Distance class in your unit tests to isolate conversions. For integration tests, verify that form submissions, model accessors, or API responses correctly convert distances using the package’s fluent methods.