Skip to content
OA.

Home / Blog / PHP & Laravel

› PHP & Laravel

How to Create a UUID Generator with PHP (Plain PHP, Laravel & ramsey/uuid)

By Ohene Adjei Effah · 3 min read

PHP UUID Generator

How to Create a UUID Generator with PHP

A UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify a record — a user, an order, an API resource — without relying on an auto-incrementing database ID. They're especially useful when you need IDs that are safe to generate client-side, hard to guess, or unique across multiple systems that don't share a database.

This post covers three ways to generate one in PHP, from no dependencies at all to Laravel's built-in helper.

What a UUID Looks Like

A standard UUID (version 4, the random variant) looks like this:

d290f1ee-6c54-4b01-90e6-d701748f0851

36 characters: 32 hex digits, 4 hyphens, and a couple of fixed bits that mark it as version 4.

Option 1 — Plain PHP, No Dependencies

If you just need a UUID v4 and don't want to add a package for it, you can generate one with PHP's built-in random_bytes():

function generateUuidV4(): string
{
    $data = random_bytes(16);

    // Set version to 0100 (UUID v4)
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
    // Set bits 6-7 to 10 (variant RFC 4122)
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);

    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

echo generateUuidV4();
// e.g. 6f9619ff-8b86-d011-b42d-00cf4fc964ff

This is fine for most use cases — it's cryptographically random and RFC 4122-compliant. The tradeoff is you're maintaining the implementation yourself, and it only covers v4. If you need other UUID versions (v1 time-based, v5 name-based, etc.), reach for a package instead.

Option 2 — The ramsey/uuid Package

For anything beyond basic v4 generation — or if you just don't want to own that function — ramsey/uuid is the standard PHP library for this.

Install it:

composer require ramsey/uuid

Use it:

use Ramsey\Uuid\Uuid;

$uuid4 = Uuid::uuid4();
echo $uuid4->toString();

// Other versions are available too:
$uuid1 = Uuid::uuid1();   // time-based
$uuid5 = Uuid::uuid5(Uuid::NAMESPACE_URL, 'https://ohene.dev'); // name-based, deterministic

uuid5 is worth knowing about specifically — it generates the same UUID every time for the same namespace + name input, which is useful when you want a stable, unique ID derived from something like a URL or email address.

Option 3 — Laravel's Built-In Helper

If you're already in a Laravel app, you don't need a package at all — Laravel ships with UUID generation via its Str helper (which itself wraps ramsey/uuid under the hood):

use Illuminate\Support\Str;

$uuid = Str::uuid();
echo $uuid->toString();

Using UUIDs as Primary Keys in Eloquent

If you want a model to use UUIDs instead of auto-incrementing IDs, add the HasUuids trait (Laravel 9+):

use Illuminate\Database\Eloquent\Concerns\HasUuids;

class Order extends Model
{
    use HasUuids;
}

And in your migration, define the column as a UUID rather than an integer:

$table->uuid('id')->primary();

Laravel will then generate a UUID automatically whenever a new Order is created — no manual assignment needed.

Which Option Should You Use?

Situation Recommended approach
Quick script, no framework, no dependencies wanted Plain PHP function (Option 1)
Non-Laravel PHP project needing multiple UUID versions ramsey/uuid (Option 2)
Laravel project Built-in Str::uuid() / HasUuids (Option 3)

A Note on UUIDs vs Auto-Increment IDs

UUIDs aren't free — they're larger (16 bytes vs 4-8 for integers), which means bigger indexes and slightly slower joins at scale. They're the right call when you need IDs that are unguessable, generated outside the database, or unique across multiple services. For a typical single-database CRUD app with no external ID generation requirement, auto-incrementing integers are often still the simpler, faster default.

Related posts