Weave Code
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
Unit Conversions

Unit Conversions Laravel Package

spatie/unit-conversions

Lightweight PHP unit conversion library by Spatie. Currently supports weight conversion from kilograms to pounds via a fluent API: Weight::fromKilograms(100)->toLbs(). Install with Composer and extend as more units are added.

View on GitHub
Deep Wiki
Context7

Perform unit conversions in PHP

Frequently asked questions about Unit Conversions
How do I convert kilograms to pounds in Laravel using this package?
Use the fluent API: `Weight::fromKilograms(100)->toLbs()` returns the converted value (e.g., 220.4623). No Laravel-specific setup is needed—it works in plain PHP or Laravel apps. Wrap it in a service class if you need dependency injection.
Does this package support other unit types besides kg→lbs?
Currently, only kilograms to pounds is supported. The package is lightweight and niche-focused, but you can extend it by subclassing `Weight` or contributing new unit types. Check the GitHub repo for updates or fork it for custom needs.
Is this package compatible with Laravel 10 and PHP 8.2?
The package has no Laravel-specific dependencies and works with PHP 7.4+. Test it in your environment for PHP 8.2 compatibility, as the last release was in 2020. If issues arise, consider a wrapper layer for stricter type hints or named arguments.
How accurate are the conversion results? Can I trust them for production?
The package uses hardcoded conversion factors (1 kg = 2.20462 lbs) with high precision for typical use cases. For critical applications (e.g., medical or aerospace), validate against industry standards or use a more specialized library. Edge cases (negative values, NaN) aren’t handled by default.
Should I use this package or write a custom conversion function?
Use this package if you need a simple, tested solution for kg→lbs conversions in Laravel. It’s ideal for lightweight projects like e-commerce or fitness apps. For broader unit support or strict precision needs, a custom function or a library like `php-math/units` might be better.
How do I integrate this into Laravel’s service container?
Bind the package to Laravel’s container by creating a service class (e.g., `UnitConverter`) and registering it in `AppServiceProvider`. Example: `$this->app->bind(UnitConverter::class, fn($app) => new UnitConverter());`. This improves testability and reusability.
Does this package work with Blade templates or frontend frameworks?
Yes, but avoid direct Blade usage for complex logic. Wrap conversions in a helper function or expose them via API endpoints. For frontend frameworks (Vue/React), fetch converted values from Laravel APIs or use a client-side math library for dynamic conversions.
Are there performance concerns for high-volume conversions?
The package is stateless and lightweight, but repeated conversions in loops may benefit from caching. Store results in Laravel’s cache or a database (e.g., Redis) if precision isn’t critical. Benchmark in your environment to assess impact.
How do I handle invalid inputs like negative weights?
The package doesn’t validate inputs by default. Add custom validation (e.g., `if ($kg < 0) throw new InvalidArgumentException`) before calling `fromKilograms()`. For reusable logic, create a wrapper class with input checks.
What’s the maintenance status? Is this package still actively developed?
The last release was in 2020, and there’s no recent activity. While stable for its current scope, assess whether its niche (kg→lbs) meets your needs. For active development, consider alternatives like `php-math/units` or a custom solution. Check GitHub for forks or community updates.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport