Getting started

First thing we will need is a fresh install of Laravel.

$ composer create-project laravel/laravel

Once that is done, we will require one more package which will be used to generate our UUIDs. The one we will be using in this tutorial is webpatser/laravel-uuid.

$ cd /path/to/project

$ composer require webpatser/laravel-uuid

Once composer finishes installing our package we’ll have just one last step left. We need to create an alias for the facade next. In order to do this, add the following line of code inside the config/app.php file where the aliases array resides.

'Uuid' => Webpatser\Uuid\Uuid::class,

You are now set to start using uuids in your Laravel application.

Migrations

Let us start on the database side of things. We are going to use the users migration that comes out by default. As is the primary key is defined like this.

$table->increments('id');

We need to change it to another type which luckily Laravel has us covered. Which will create a char(36) inside of our database schema.

$table->uuid('id');

Since we remove the increment type the schema builder doesn’t know that it will be a primary key so we need to add that manually.

$table->primary('id');

Models

Removing auto increment

Laravel out of box will try and auto increment the Primary Key when you try and create a new user but luckily, we can turn off this feature but adding the following attribute in our model, in our case it’s the User

/**

* Indicates if the IDs are auto-incrementing.

*

* @var bool

*/

public $incrementing = false;

Creating a trait

We are going to have take advantage of the events, to be more specific we are going to use the ‘creating’ event. This event is triggered exactly when a new record of the model is getting created.

One of the cleaner way is to extract it into a trait by itself and do all the logic there then you can just use that trait when you need a model to use UUIDs.

namespace App;



use Webpatser\Uuid\Uuid; trait Uuids

{



/**

* Boot function from laravel.

*/

protected static function boot()

{

parent::boot();



static::creating(function ($model) {

$model->{$model->getKeyName()} = Uuid::generate()->string;

});

}

}

Now what is this doing? We are hooking into the boot method of the model and when the ‘creating’ model is called we pass in that closure. The method ‘getKeyName’ will get the name of the primary key, just in case it is you changed into something else then ‘id’

static::creating(function ($model) {

$model->{$model->getKeyName()} = Uuid::generate()->string;

});

Using the trait

Now we can go inside our User model and use that trait.

use Uuids;

Almost done

Now all we have to do is create a new user to see if work!

Route::get('/', function () {

return User::create([

'name' => 'Jane',

'email' => 'john@jane.com',

'password' => bcrypt('password'),

]);

});

If you go to that route inside of the browser you will see something like the following.

{

“name”: “Jane”,

”email”: “john@jane.com”,

”id”: “b14e5c00-e899–11e5–9c37-df062687ba4f”,

”updated_at”: “2016–03–12 21:30:50”,

”created_at”: “2016–03–12 21:30:50”

}

You can check out a working example on my github account here