2017 © Pedro Peláez
 

library eloquent-uuid

A Laravel Eloquent Model trait for using UUID's as primary keys

image

alsofronie/eloquent-uuid

A Laravel Eloquent Model trait for using UUID's as primary keys

  • Monday, March 19, 2018
  • by alsofronie
  • Repository
  • 5 Watchers
  • 86 Stars
  • 53,500 Installations
  • PHP
  • 17 Dependents
  • 0 Suggesters
  • 20 Forks
  • 3 Open issues
  • 10 Versions
  • 11 % Grown

The README.md

eloquent-uuid

An Eloquent UUID Trait to use with Laravel 5.1 - 5.4, (*1)

MIT licensed Total Downloads, (*2)

It should work with Laravel 5.0 also, but it's untested., (*3)

The trait overwrites the static boot method and listens to the creating event. It generates a UUID (strips the dashes) and stores it in the primary key attribute. Thus, you'll need a CHAR(32) primary key for your model (see migrations below)., (*4)

Installation

composer require alsofronie/eloquent-uuid:dev-master

Use

In order to make it faster, you have the option to use one of three traits:, (*5)

  • UuidModelTrait - the key must be CHAR(36) and contains the dashes
  • Uuid32ModelTrait - the key must be CHAR(32), the dashes are stripped
  • UuidBinaryModelTrait - the key is BINARY(16).

Using UuidModelTrait

In order to use this trait, your schema must be something like:, (*6)

<?php
    // ...
    Schema::create('users', function (Blueprint $table) {
        $table->uuid('id'); // this will create a CHAR(36) field
        // or
        // $table->char('id', 36);
        $table->string('username', 32);
        $table->string('password', 50);
        // ...
        $table->primary('id');
    });

Using Uuid32ModelTrait

For this type, just use CHAR(32) in your schema (this is identical to the first one, but with stripped dashes)., (*7)

char('id', 32);
        // ...
        $table->string('username', 32);
        $table->string('password', 50);

        $table->primary('id');
    });
```

#### Using `UuidBinaryModelTrait`

This stores the key as binary. The default Laravel `Blueprint` curretly
[does not currently support binary fields with specified length](https://github.com/laravel/framework/issues/1606),
and (at least in MySQL) you cannot create an index (including primary key) on a `BINARY` field without length.

So, the schema definition should be something like this (please double check if you're not using MySQL):

```
string('username', 32);
        $table->string('password', 50);
    });

    DB::statement('ALTER TABLE `usersb` ADD `id` BINARY(16); ALTER TABLE `usersb` ADD PRIMARY KEY (`id`);')
?>

There are two additional notes for this particular trait., (*8)

Note 1. In order to get a string representation of your uuid, simple call $model->id_string and you'll get it., (*9)

Note 2. You can use User::find($uuid) with both the binary version or the string (bin2hex) version., (*10)

Using the optimized uuid

To use the optimized uuid, put the following line in your models: private static $uuidOptimization = true;, (*11)

In your models

In order to use this in your models, just put use Uuid[32|Binary]ModelTrait;:, (*12)

<?php

namespace App;
use Alsofronie\Uuid\Uuid[32|Binary]ModelTrait;

class User extends Eloquent
{
    use Uuid[32|Binary]ModelTrait;
}

Running tests

To run the tests, just run composer install and ./vendor/bin/phpunit., (*13)

The Versions

19/03 2018

dev-master

9999999-dev

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

20/07 2017

v1.0.9

1.0.9.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

20/07 2017

v1.0.8

1.0.8.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

08/05 2017

v1.0.7

1.0.7.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

13/03 2017

v1.0.6

1.0.6.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

22/10 2016

v1.0.5

1.0.5.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

16/03 2016

v1.0.3

1.0.3.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

10/01 2016

v1.0.2

1.0.2.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

15/12 2015

v1.0.1

1.0.1.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie

15/12 2015

v1.0

1.0.0.0

A Laravel Eloquent Model trait for using UUID's as primary keys

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alex Sofronie