Wednesday 8 November 2017

How to generate UUID in Laravel 5 ?

What is UUID?

Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUIDs are built-in.

Installation

In Laravel 5.5 laravel-uuid will install via the new package discovery feature so you only need to add the package to your composer.json file


composer require "webpatser/laravel-uuid:^3.0"
after installation, you should see

Discovered Package: webpatser/laravel-uuid

and you are ready to go 

Usage

To quickly generate a UUID just do
Uuid::generate()
This will generate a version 1 Uuid object with a random generated MAC address.
To echo out the generated UUID, cast it to a string
(string) Uuid::generate()
or
Uuid::generate()->string

Advanced Usage

UUID creation

Generate a version 1, time-based, UUID. You can set the optional node to the MAC address. If not supplied it will generate a random MAC address.
Uuid::generate(1,'00:11:22:33:44:55');
Generate a version 3, name-based using MD5 hashing, UUID
Uuid::generate(3,'test', Uuid::NS_DNS);
Generate a version 4, truly random, UUID
Uuid::generate(4);
Generate a version 5, name-based using SHA-1 hashing, UUID
Uuid::generate(5,'test', Uuid::NS_DNS);

Some magic features

To import a UUID
$uuid = Uuid::import('d3d29d70-1d25-11e3-8591-034165a3a613');
Extract the time for a time-based UUID (version 1)
$uuid = Uuid::generate(1);
dd($uuid->time);
Extract the version of an UUID
$uuid = Uuid::generate(4);
dd($uuid->version);


No comments:

Post a Comment

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles