PHP code example of jobmetric / laravel-unit

1. Go to this page and download the library: Download jobmetric/laravel-unit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jobmetric / laravel-unit example snippets


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' => 'کیلوگرم',
        ],
    ],
]);

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);

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

$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();
bash
php artisan migrate
bash
php artisan vendor:publish --provider="JobMetric\\UnitConverter\\UnitConverterServiceProvider"
bash
# 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