Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Distance Laravel Package

frost/distance

PHP distance conversion library for US and metric units. Convert between mi, ft, in, km, m, cm, and mm (US↔metric, US↔US, metric↔metric). Simple fluent API: setUnit(), setDistance(), convertTo().

View on GitHub
Deep Wiki
Context7

A library to set and convert distances

Frequently asked questions about Distance
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle