Laravel: Using UUID’s as Primary keys with Eloquent Models

Mir Adnan
3 min readNov 12, 2020

Most developers prefer numerical primary keys because they auto increment, they are efficient to use and easy to generate. But that doesn’t mean that a primary key has to be a number.

What is a UUID:

UUID stands for Universally Unique Identifier. UUID is defined based on RFC 4122, “a Universally Unique Identifier (UUID) URN Namespace).

UUID is designed as a number that is unique globally in space and time. Two UUID values are expected to be distinct, even they are generated on two independent servers.

A UUID value is a 128-bit number represented as a UTF8 string of five hexadecimal numbers in the following format give below

123e4567-e89b-12d3-a456-426614174000

Pros

Using UUID as a primary key brings the following advantages:

  • UUID’s are unique across tables, databases, and even servers. This allows us to merge rows from different databases or distribute databases across multiple servers.
  • UUID’s are safer to use in URLs. For example, if the database has a user with id 10https://example.com/user/10 it is easy to guess that there is a customer 11, 12, etc., and this could be a target for an attack.
  • There are uses cases where we need the primary key “id” of a table to be generated before we process a business logic. In such cases UUID’s are handy.
  • UUID values can be generated anywhere that avoid a round trip to the database server. It also simplifies logic in the application. For example, to insert data into a parent table and child tables, you have to insert into the parent table first, get generated id and then insert data into the child tables. By using UUID, you can generate the primary key value of the parent table up front and insert rows into both parent and child tables at the same time within a transaction.

Cons

  • The most obvious one is its size. It’s 4 times larger than a numerical ID and can’t be handled as efficiently. You should, therefore, decide carefully if you want to use UUID’s or numeric ID’s and discuss it with your database administrator.
  • Debugging seems to be more difficult, you SQL queries will change from
SELECT * FROM users WHERE id = 10;TOSELECT * FROM users WHERE id = '123e4567-e89b-12d3-a456-426614174000';
  • You cannot do ORDER BY id ASC or ORDER BY id DESC anymore. You’ll have to rely on other date & time fields to sort data.

Implementing in your Laravel application

Create a folder in your app/Models directory name as Concerns. Create a PHP class file UsesUuid.php . Copy and paste the below code.

<?php


namespace App\Models\Concerns;

use Illuminate\Support\Str;

trait UsesUuid
{
protected static function bootUsesUuid()
{
static::creating(function ($model) {
if (!$model->getKey()) {
$model->{$model->getKeyName()} = (string)Str::uuid();
}
});
}

public function getIncrementing()
{
return false;
}

public function getKeyType()
{
return 'string';
}
}

Now in your Model class file.

<?phpnamespace App\Models\User;use App\Model\Concerns;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticable
{
uses Concerns/UsesUuid;}

Note:

  • The above code is tested with Laravel 5.8 ≤ 8.
  • Tested with MySQL, PostgreSQL and MongoDB.

Thats it!

Your user’s model will now be using UUID’s as primary key.

--

--