jobmetric/laravel-unit
Composer 安装命令:
composer require jobmetric/laravel-unit
包简介
This is a unit management package for Laravel that you can use in your projects.
README 文档
README
Laravel Unit Converter
Unit Management for Laravel. Flexible. Precise.
Laravel Unit Converter helps you manage and convert measurement units in a clean, consistent way—from Weight and Length to Currency, Volume, Temperature, and 30+ other unit types. It is designed to be used as a reusable package in real-world Laravel applications where unit conversion needs to be normalized and shared across multiple models.
Why Laravel Unit Converter?
Comprehensive Unit Type Support
Support for 30+ unit types out of the box:
- Physical Units: Weight, Length, Volume, Area, Temperature, Pressure, Speed, Force, Energy, Power, and more
- Digital Units: Data Storage, Data Transfer
- Scientific Units: Electric Current, Voltage, Resistance, Capacitance, Inductance, Magnetic Flux, Radiation
- Everyday Units: Currency, Time, Cooking measurements, Fuel Consumption
Flexible Base Unit System
Each unit type has a base unit (value = 1) that serves as the conversion reference. All other units in the same type are defined relative to this base, making conversions accurate and consistent.
Service-first API + Facade
The UnitConverter service provides a complete API for:
- Creating and managing units
- Converting values between units
- Changing base units dynamically
- Tracking unit usage across your application
Quick Start
Install via Composer:
composer require jobmetric/laravel-unit-converter
Run migrations:
php artisan migrate
Optionally publish config/translations (if you need to override defaults):
php artisan vendor:publish --provider="JobMetric\\UnitConverter\\UnitConverterServiceProvider"
Usage (Examples)
Store a unit using the Facade:
use JobMetric\UnitConverter\Facades\UnitConverter; $response = UnitConverter::store([ 'type' => 'weight', 'value' => 1000, // 1 kilogram = 1000 grams (if gram is base) 'status' => true, 'translation' => [ 'en' => [ 'name' => 'Kilogram', 'code' => 'kg', ], 'fa' => [ 'name' => 'کیلوگرم', 'code' => 'کیلوگرم', ], ], ]);
Convert between units:
use JobMetric\UnitConverter\Facades\UnitConverter; // Convert 5 kilograms to grams $result = UnitConverter::convert($kilogramUnitId, $gramUnitId, 5); // Result: 5000 // Or use the helper function $result = unitConvert($kilogramUnitId, $gramUnitId, 5);
Attach units to your models using traits:
use Illuminate\Database\Eloquent\Model; use JobMetric\UnitConverter\HasUnit; class Product extends Model { use HasUnit; protected array $unitables = [ 'weight' => 'weight', 'length' => 'length', 'width' => 'length', 'height' => 'length', ]; }
Assign units to a model:
$product->fill([ 'unit' => [ 'weight' => ['unit_id' => 1, 'value' => 2.5], 'length' => ['unit_id' => 2, 'value' => 30], 'width' => ['unit_id' => 2, 'value' => 20], 'height' => ['unit_id' => 2, 'value' => 10], ] ]); $product->save();
Available Artisan Commands
# List all registered units php artisan unit:list # Convert a value between units php artisan unit:convert # Export units to file php artisan unit:export # Seed default units php artisan unit:seed
Documentation
Documentation for Laravel Unit Converter is available here:
The documentation includes:
- Getting Started - Installation and configuration
- Traits -
HasUnitfor attaching units to models - Services & Facades - Complete API reference for UnitConverter
- Unit Types - All 30+ supported unit types
- Requests & Resources - Validation and API responses
- Events - Hook into lifecycle events (
UnitStoreEvent,UnitUpdateEvent,UnitDeleteEvent) - Helper Functions -
unitConvert()and more - Testing - How to run package tests and expected patterns
Contributing
Thank you for participating in laravel-unit-converter. A contribution guide can be found here.
License
The laravel-unit-converter is open-sourced software licensed under the MIT license. See License File for more information.
jobmetric/laravel-unit 适用场景与选型建议
jobmetric/laravel-unit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「conversion」 「management」 「unit」 「converter」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jobmetric/laravel-unit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jobmetric/laravel-unit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jobmetric/laravel-unit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Fixer.io data provider for Peso
A Silverstripe module to allow you to log into any of your Silverstripe sites from one place.
Task system for APPUI
Color conversion library
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-11