1. Go to this page and download the library: Download khaledalam/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/ */
KhaledAlam\Unit\Quantity;
// Common units are auto-registered — just use them.
$length1 = Quantity::of(2.0, 'm');
$length2 = Quantity::of(100.0, 'cm');
$sum = $length1->add($length2); // auto-converts cm -> m
echo $sum->to('m'); // "3 m"
use Illuminate\Database\Eloquent\Model;
use KhaledAlam\Unit\Laravel\AsQuantity;
class Parcel extends Model
{
protected function casts(): array
{
return ['weight' => AsQuantity::class];
}
}
$parcel->weight = Quantity::of(2.5, 'kg'); // stored as "2.5 kg"
$parcel->weight->to('g'); // 2500 g
$parcel->weight = '5 ft 3 in'; // strings are parsed on the way in
return [
'units' => [
new \KhaledAlam\Unit\Unit('furlong', 'fur', 201.168, new \KhaledAlam\Unit\Dimension(['L' => 1])),
],
];
use Doctrine\DBAL\Types\Type;
use KhaledAlam\Unit\Doctrine\QuantityType;
Type::addType(QuantityType::NAME, QuantityType::class); // e.g. in a bundle boot / bootstrap
use Doctrine\ORM\Mapping as ORM;
#[ORM\Column(type: 'quantity', nullable: true)]
public ?Quantity $weight = null;