How To Generate an UUID in Laravel?

How To Generate an UUID in Laravel

Universally Unique Identifier or UUID as it is popularly known is a 128-bit number that is generally used to identify unique information. Being 36-character long, a UUID is considered a safe bet in generating uniquely identifiable strings every time.

Generating UUID in Laravel

Laravel can generate UUIDs out of the box and ready to be used.

The following code will generate a UUID in Laravel.

$generatedUUID      = Str::uuid();

That’s it. This will return a UUID.

UUID generated by the code above

Class ‘App\Http\Controllers\Str’ not found

If you come across the above-mentioned error, this means that you not added the correct references. In order to fix this error, add the following to the top of your controller.

use Illuminate\Support\Str;

In order to access it as a sting, we can use it as below:

(string) Str::orderedUuid() 

A working example of the code can be found here.

Share This Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore