Skip to main content

Laravel Unit Converter Showcase

Discover real-world examples and use cases of Laravel Unit Converter in action. See how different applications leverage unit management to handle measurements, conversions, and multi-unit data storage professionally.

Understanding Unit Converter Components

Laravel Unit Converter provides powerful features for managing measurement units:

  • Unit Types: Over 30 built-in types including weight, length, volume, temperature, currency, and more
  • Base Unit System: Each type has a base unit (value = 1) for accurate conversions
  • HasUnit Trait: Core trait that adds unit functionality to any Eloquent model
  • Polymorphic Relations: Single table for unit relations across all model types
  • Translation Support: Multilingual unit names, codes, and descriptions
  • Query Scopes: Filter models by unit attributes
  • Artisan Commands: CLI tools for managing and converting units

E-Commerce Product Management

Product Weight & Dimensions

Complete product measurement management for e-commerce

Overview

E-commerce platforms need to store product dimensions and weights for shipping calculations, inventory management, and product specifications. Laravel Unit Converter allows you to store these measurements in any unit the supplier provides, then convert them on-demand for shipping carriers or customer display.

Product Measurements
  • Weight (kg, g, lb, oz)
  • Length, width, height (m, cm, in, ft)
  • Volume for liquids (L, mL, gal)
  • Area for fabrics (m², ft²)
  • Supplier-provided units
  • Shipping carrier units
Business Benefits
  • Accurate shipping calculations
  • Flexible unit input
  • Automatic conversions
  • Multi-regional support
  • Carrier integration ready
  • Customer-friendly display

Implementation Example

namespace App\Models;

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',
'volume' => 'volume',
];

protected $fillable = ['name', 'sku', 'price'];
}

// Store product with measurements
$product = Product::create([
'name' => 'Wireless Headphones',
'sku' => 'WH-001',
'price' => 99.99,
]);

// Store measurements in supplier's units
$product->storeUnitBatch([
'weight' => ['unit_id' => $gramId, 'value' => 250],
'length' => ['unit_id' => $centimeterId, 'value' => 18],
'width' => ['unit_id' => $centimeterId, 'value' => 16],
'height' => ['unit_id' => $centimeterId, 'value' => 8],
]);

// Get weight in different units for shipping
$weightKg = $product->getUnit('weight', $kilogramId);
// => ['unit' => Unit, 'value' => 0.25, 'translation' => ['name' => 'Kilogram', ...]]

$weightLb = $product->getUnit('weight', $poundId);
// => ['unit' => Unit, 'value' => 0.551, 'translation' => ['name' => 'Pound', ...]]

// Calculate shipping dimensions in inches for US carrier
$lengthInches = $product->getUnit('length', $inchId);
$widthInches = $product->getUnit('width', $inchId);
$heightInches = $product->getUnit('height', $inchId);

Scientific Data Management

Laboratory Measurements

Precise unit handling for scientific applications

Overview

Scientific and laboratory applications require precise unit handling with support for specialized measurements like pressure, temperature, concentration, and energy. Laravel Unit Converter provides high-precision decimal handling and support for scientific unit types.

Scientific Units
  • Temperature (°C, °F, K)
  • Pressure (Pa, bar, psi, atm)
  • Energy (J, kJ, cal, kWh)
  • Concentration (mol/L, ppm)
  • Viscosity (Pa·s, cP)
  • Radiation (Bq, Sv, Gy)
Application Benefits
  • High precision (10 decimal places)
  • 30+ unit types supported
  • Consistent calculations
  • Audit trail with events
  • Export capabilities
  • API-ready responses

Implementation Example

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use JobMetric\UnitConverter\HasUnit;

class LabSample extends Model
{
use HasUnit;

protected array $unitables = [
'temperature' => 'temperature',
'pressure' => 'pressure',
'concentration' => 'concentration',
'volume' => 'volume',
];
}

// Record a lab sample with measurements
$sample = LabSample::create(['name' => 'Sample A-001']);

$sample->storeUnitBatch([
'temperature' => ['unit_id' => $celsiusId, 'value' => 25.5],
'pressure' => ['unit_id' => $pascalId, 'value' => 101325],
'concentration' => ['unit_id' => $molPerLiterId, 'value' => 0.5],
'volume' => ['unit_id' => $milliliterId, 'value' => 100],
]);

// Convert for different display requirements
$tempFahrenheit = $sample->getUnit('temperature', $fahrenheitId);
// => ['value' => 77.9, ...]

$pressureBar = $sample->getUnit('pressure', $barId);
// => ['value' => 1.01325, ...]

// Use helper function for quick conversion
$tempKelvin = unitConvert($celsiusId, $kelvinId, 25.5);
// => 298.65

Recipe & Cooking Application

Recipe Ingredients

Flexible ingredient measurements for cooking applications

Overview

Recipe and cooking applications need to handle various measurement units—cups, tablespoons, grams, ounces, and more. Laravel Unit Converter includes a dedicated "cooking" unit type that supports common kitchen measurements, with automatic conversions between metric and imperial systems.

Cooking Units
  • Cups, tablespoons, teaspoons
  • Grams, ounces, pounds
  • Milliliters, liters
  • Fluid ounces
  • Pinch, dash (small amounts)
  • Regional variations
User Experience Benefits
  • User-preferred unit display
  • Recipe scaling support
  • Metric/Imperial switching
  • Ingredient substitution
  • Nutritional calculations
  • Shopping list generation

Implementation Example

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use JobMetric\UnitConverter\HasUnit;

class RecipeIngredient extends Model
{
use HasUnit;

protected array $unitables = [
'amount' => 'cooking',
'weight' => 'weight',
'volume' => 'volume',
];

protected $fillable = ['recipe_id', 'ingredient_name'];
}

// Add ingredient with cooking measurements
$ingredient = RecipeIngredient::create([
'recipe_id' => 1,
'ingredient_name' => 'All-Purpose Flour',
]);

$ingredient->storeUnit('amount', $cupId, 2.5);

// Convert to metric for precision baking
$flourGrams = $ingredient->getUnit('amount', $gramId);
// => ['value' => 312.5, ...] (assuming 1 cup flour = 125g)

// Display in user's preferred unit system
$userPreferredUnit = auth()->user()->preferred_cooking_unit ?? $cupId;
$displayAmount = $ingredient->getUnit('amount', $userPreferredUnit);

// Scale recipe (multiply by scaling factor)
$scalingFactor = 1.5; // Making 1.5x the recipe
$scaledValue = $ingredient->getUnit('amount')['value'] * $scalingFactor;
// => 3.75 cups

Currency Exchange System

Multi-Currency Pricing

Dynamic currency conversion for international commerce

Overview

International e-commerce and financial applications need to handle multiple currencies with dynamic exchange rates. Laravel Unit Converter's currency type allows you to store prices in one currency and convert to any other currency on-demand. Update exchange rates centrally and all conversions automatically reflect the new rates.

Currency Features
  • All major world currencies
  • Cryptocurrency support
  • Dynamic exchange rates
  • Base currency flexibility
  • Historical rate tracking
  • Multi-decimal precision
Business Benefits
  • Global pricing support
  • Automatic conversions
  • Centralized rate management
  • Localized price display
  • Financial reporting
  • Payment gateway integration

Implementation Example

use JobMetric\UnitConverter\Facades\UnitConverter;

// Create currency units with exchange rates (base: USD = 1)
UnitConverter::store([
'type' => 'currency',
'value' => 1,
'status' => true,
'translation' => [
'en' => ['name' => 'US Dollar', 'code' => 'USD', 'position' => 'left'],
],
]);

UnitConverter::store([
'type' => 'currency',
'value' => 0.92, // 1 USD = 0.92 EUR
'status' => true,
'translation' => [
'en' => ['name' => 'Euro', 'code' => 'EUR', 'position' => 'left'],
],
]);

UnitConverter::store([
'type' => 'currency',
'value' => 0.000023, // 1 USD = ~43,500 IRR
'status' => true,
'translation' => [
'en' => ['name' => 'Iranian Rial', 'code' => 'IRR', 'position' => 'right'],
],
]);

// Product with price in base currency
class Product extends Model
{
use HasUnit;

protected array $unitables = [
'price' => 'currency',
];
}

$product = Product::find(1);
$product->storeUnit('price', $usdId, 99.99);

// Display price in user's currency
$priceEur = $product->getUnit('price', $euroId);
// => ['value' => 91.99, 'translation' => ['code' => 'EUR', ...]]

$priceIrr = $product->getUnit('price', $irrId);
// => ['value' => 4347826, 'translation' => ['code' => 'IRR', ...]]

// Update exchange rate and all conversions automatically update
$euro = UnitConverter::getObject($euroId);
UnitConverter::update($euroId, ['value' => 0.94]); // Rate changed

Data Center Capacity Planning

Server & Storage Metrics

Data storage and transfer measurements for IT infrastructure

Overview

IT infrastructure and hosting applications need to track data storage capacity and transfer rates. Laravel Unit Converter includes dedicated unit types for data storage (bytes, KB, MB, GB, TB, PB) and data transfer (bps, Kbps, Mbps, Gbps) with proper binary and decimal prefix support.

Data Units
  • Storage: B, KB, MB, GB, TB, PB
  • Transfer: bps, Kbps, Mbps, Gbps
  • Binary prefixes (KiB, MiB, GiB)
  • Decimal prefixes (kB, MB, GB)
  • Bits and bytes variants
  • Network bandwidth units
Infrastructure Benefits
  • Capacity planning accuracy
  • Bandwidth monitoring
  • Usage reporting
  • Billing calculations
  • Resource allocation
  • Performance tracking

Implementation Example

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use JobMetric\UnitConverter\HasUnit;

class Server extends Model
{
use HasUnit;

protected array $unitables = [
'storage_capacity' => 'data_storage',
'memory' => 'data_storage',
'bandwidth' => 'data_transfer',
];

protected $fillable = ['name', 'hostname', 'status'];
}

// Record server specifications
$server = Server::create([
'name' => 'Web Server 1',
'hostname' => 'web01.example.com',
]);

$server->storeUnitBatch([
'storage_capacity' => ['unit_id' => $terabyteId, 'value' => 2],
'memory' => ['unit_id' => $gigabyteId, 'value' => 64],
'bandwidth' => ['unit_id' => $gbpsId, 'value' => 1],
]);

// Display in different formats
$storageGb = $server->getUnit('storage_capacity', $gigabyteId);
// => ['value' => 2048, ...] (2 TB = 2048 GB)

$bandwidthMbps = $server->getUnit('bandwidth', $mbpsId);
// => ['value' => 1000, ...] (1 Gbps = 1000 Mbps)

// Calculate usage percentage
$usedStorage = 1.5; // TB
$capacityTb = $server->getUnit('storage_capacity')['value'];
$usagePercent = ($usedStorage / $capacityTb) * 100;
// => 75%

Explore More

These examples demonstrate just a fraction of what's possible with Laravel Unit Converter. The package supports over 30 unit types, each with multiple units:

  • Physical: Weight, Length, Area, Volume, Temperature, Pressure, Speed, Force, Energy, Power
  • Electrical: Voltage, Current, Resistance, Capacitance, Inductance
  • Scientific: Density, Viscosity, Concentration, Radiation, Luminosity
  • Digital: Data Storage, Data Transfer
  • Other: Time, Angle, Currency, Cryptocurrency, Cooking