2017 © Pedro Peláez
 

library laravel-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.

image

webpatser/laravel-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.

  • Tuesday, May 15, 2018
  • by webpatser
  • Repository
  • 28 Watchers
  • 1070 Stars
  • 2,009,527 Installations
  • PHP
  • 75 Dependents
  • 0 Suggesters
  • 101 Forks
  • 6 Open issues
  • 20 Versions
  • 11 % Grown

The README.md

Laravel Uuid

Total Downloads Build Status codecov.io Latest Stable Version Licence, (*1)

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., (*2)

What's new in 4.*

Laravel-uuid is now ready for Laravel 8. It has the same requirements so that means PHP 7.3 or PHP 8. Laravel package auto-discovery is enabled, and you can now use the UUID validation. Validation examples are below and in the tests., (*3)

For older Laravel or PHP versions use older versions; see below..., (*4)

What's new in 3.*

Laravel-uuid is now refactored for Laravel 5.5. It has the same requirements so that means PHP 7. Laravel package auto-discovery is enabled, and you can now use the UUID validation. Validation examples are below and in the tests., (*5)

Laravel 5.0, 5.1, 5.2, 5.3 and 5.4? use version 2, (*6)

Laravel 4.*? use version 1, (*7)

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, (*8)

composer require "webpatser/laravel-uuid:^3.0"

after installation you should see, (*9)

Discovered Package: webpatser/laravel-uuid

and you are ready to go, (*10)

Basic Usage

To quickly generate a UUID just do, (*11)

Uuid::generate()

This will generate a version 1 Uuid object with a random generated MAC address., (*12)

To echo out the generated UUID, cast it to a string, (*13)

(string) Uuid::generate()

or, (*14)

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., (*15)

Uuid::generate(1,'00:11:22:33:44:55');

Generate a version 3, name-based using MD5 hashing, UUID, (*16)

Uuid::generate(3,'test', Uuid::NS_DNS);

Generate a version 4, truly random, UUID, (*17)

Uuid::generate(4);

Generate a version 5, name-based using SHA-1 hashing, UUID, (*18)

Uuid::generate(5,'test', Uuid::NS_DNS);

Some magic features

To import a UUID, (*19)

$uuid = Uuid::import('d3d29d70-1d25-11e3-8591-034165a3a613');

Extract the time for a time-based UUID (version 1), (*20)

$uuid = Uuid::generate(1);
dd($uuid->time);

Extract the version of an UUID, (*21)

$uuid = Uuid::generate(4);
dd($uuid->version);

Eloquent UUID generation

If you want an UUID magically be generated in your Laravel models, just add this boot method to your Model., (*22)

/**
 *  Setup model event hooks
 */
public static function boot()
{
    parent::boot();
    self::creating(function ($model) {
        $model->uuid = (string) Uuid::generate(4);
    });
}

This will generate a version 4 UUID when creating a new record., (*23)

Model binding to UUID instead of primary key

If you want to use the UUID in URLs instead of the primary key, you can add this to your model (where 'uuid' is the column name to store the UUID), (*24)

/**
 * Get the route key for the model.
 *
 * @return string
 */
public function getRouteKeyName()
{
    return 'uuid';
}

When you inject the model on your resource controller methods you get the correct record, (*25)

public function edit(Model $model)
{
   return view('someview.edit')->with([
        'model' => $model,
    ]);
}

Validation

Just use like any other Laravel validator., (*26)

'uuid-field' => 'uuid', (*27)

Or create a validator from scratch. In the example an Uuid object in validated. You can also validate strings $uuid->string, the URN $uuid->urn or the binary value $uuid->bytes, (*28)

$uuid = Uuid::generate();
$validator = Validator::make(['uuid' => $uuid], ['uuid' => 'uuid']);
dd($validator->passes());

Notes

Full details on the UUID specification can be found on http://tools.ietf.org/html/rfc4122., (*29)

The Versions

15/05 2018

dev-master

9999999-dev https://github.com/webpatser/laravel-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.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

uuid rfc4122

25/10 2017

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

04/10 2017

dev-develop

dev-develop https://github.com/webpatser/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.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

uuid rfc4122

10/09 2017

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

10/09 2017

1.5.1

1.5.1.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

10/09 2017

2.2.1

2.2.1.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

04/09 2017

2.2

2.2.0.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

04/09 2017

3.0.1

3.0.1.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

uuid rfc4122

04/09 2017

3.0

3.0.0.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

uuid rfc4122

30/08 2017

2.1.1

2.1.1.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

30/08 2017

2.1

2.1.0.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

26/06 2017

2.0.3

2.0.3.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

19/05 2016

2.0.2

2.0.2.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

09/05 2016

2.0.1

2.0.1.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

15/06 2015

2.0

2.0.0.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

15/06 2015

1.5

1.5.0.0 https://github.com/webpatser/uuid

Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

uuid rfc4122

20/02 2015

1.4

1.4.0.0 https://github.com/webpatser/laravel-uuid

Laravel package to generate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

laravel uuid rfc4122

29/09 2014

1.3

1.3.0.0 https://github.com/webpatser/laravel-uuid

Laravel package to generate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

laravel uuid rfc4122

27/06 2014

1.2

1.2.0.0 https://github.com/webpatser/laravel-uuid

Laravel package to generate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

laravel uuid rfc4122

14/09 2013

1.0

1.0.0.0 https://github.com/webpatser/laravel-uuid

Laravel package to generate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

laravel uuid rfc4122