limanweb/uuid is a Laravel package for custom uuid generate and analyze functions.
It currently has 1 GitHub stars and 5.007 downloads on Packagist (latest version v1.1.1).
Install it with composer require limanweb/uuid.
Discover more Laravel packages by limanweb
or browse all Laravel packages to compare alternatives.
Last updated
Package provides any functions to generate and analyze UUID.
There used custom algorithm to generate UUID with next structure:
SSSSSSSS-UUUU-UAAA-EEEE-RRRRRRRRRRRR
S - 8 hex digits is seconds value of UUID generating timestampU - 5 hex digits is microseconds value of UUID generating timestampA - 3 hex digits is custom application codeE - 4 hex digits is custom entity codeR - 12 hex digits is random valueYou can define any integer entity code and/or application code when call genUuid(). This allows you to distinguish visually and programmatically between UUIDs created by different applications for different DB entities.
Run command to install a package into you project
composer require limanweb/uuid
Syntax:
genUuid(int $entityCode = null, int $appCode = null) : string
Returns UUID-string.
Params:
$entityCode - custom integer code of entity (is 0 by default). Max value is 65535$appCode - custom integer code of application (is 0 by default). Max value is 4095Returns UUID string.
Example:
$uuid = \Limanweb\Uuid\Support\Uuid::genUuid(256, 16);
echo $uuid; // "5ec004c4-0db2-0010-0100-a08cc1dd9a2b"
Syntax:
getUuidTimestamp(string $uuid, string $format = 'Carbon') : mixed
Retrieve timestamp from UUID generated by genUuid().
Params:
$uuid - analyzed UUID;$format - format of returning timestamp value. You can use one of tree variants:
Carbon (default) to get timestamp as \Carbon\Carbon object;DateTime to get timestamp as \DateTime object;format() to get timestamp as formatted string. For example: 'Y-m-d H:i:s.u'Example:
$uuid = "5ec004c4-0db2-0010-0100-a08cc1dd9a2b";
$ts = \Limanweb\Uuid\Support\Uuid::getUuidTimestamp($uuid, "Y-m-d H:i:s.u");
echo $ts; // "2020-05-16 18:20:36.056096"
Syntax:
getUuidAppCode(string $uuid) : int
Retrieve application code from UUID generated by genUuid().
Params:
$uuid - analyzed UUID;Example:
$uuid = "5ec004c4-0db2-0010-0100-a08cc1dd9a2b";
$appCode = \Limanweb\Uuid\Support\Uuid::getUuidAppCode($uuid);
echo $appCode; // 16
Syntax:
getUuidAppCode(string $uuid) : int
Retrieve application code from UUID generated by genUuid().
Params:
$uuid - analyzed UUID;Example:
$uuid = "5ec004c4-0db2-0010-0100-a08cc1dd9a2b";
$appCode = \Limanweb\Uuid\Support\Uuid::getUuidAppCode($uuid);
echo $appCode; // 256
Add trait \Limanweb\Uuid\Models\Concerns\UsesUuids use declaration into models where primary key is UUID.
You can define $appCode and $entityCode protected properties in your model to generate model UUID-key with specific segments.
This trait
getIncrementing() and getKeyType() methods therefore you don't need to define properties $incrementing and $keyType;getAppCode() and getEntityCode() public methods;Example:
class MyModel extends Model
{
use \Limanweb\Uuid\Models\Concerns\UsesUuids;
protected $appCode = 16; // 010
protected $entityCode = 256; // 0100
...
All IDs generated for this model will match the pattern ########-####-#010-0100-############.