Amr
Amr

Reputation: 5159

How to generate UUID without dashes (hyphens) in Laravel?

When generating a UUID in Laravel, It's being generated according to the standard format which includes 4 dashes (hyphens) like this (for example):

51a0cb84-8b3d-43c4-bfd4-8fcef1f360d4

How to generate the UUID in Laravel without dashes or hyphens? like this:

51a0cb848b3d43c4bfd48fcef1f360d4

Upvotes: 0

Views: 6695

Answers (1)

Amr
Amr

Reputation: 5159

In Laravel (since version 5.6), you can generate a UUID (version 4) that follows the standard format using the Str::uuid() method:

use Illuminate\Support\Str;

return Str::uuid();

Or, to generates a "timestamp first" UUID that may be efficiently stored in an indexed database column, you may use Str::orderedUuid():

use Illuminate\Support\Str;

return Str::orderedUuid();

And because Laravel actually makes use of ramsey/uuid package to generate the UUID, then generating the UUID in Laravel without dashes or hyphens could be done using the package's getHex() method:

use Illuminate\Support\Str;

return Str::uuid()->getHex();

// OR
return Str::orderedUuid()->getHex();

Upvotes: 8

Related Questions