Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel UUID generation

Tags:

php

uuid

laravel

I am trying to generate a UUID (not as primary key, just generate one) with the laravel-uuid package. The docs are pretty straightforward, so according to the readme file a UUID should be generated just by calling $uuid = Uuid::generate();, but it returns an empty object. (I also tried $uuid = Uuid::generate(1);)

I followed the installation instructions as provided there (nothing out of the ordinary), the app doesn't throw any errors, so I guess everything is right.

Alternative packages for this are also welcome.

like image 577
Skatch Avatar asked Jun 21 '16 15:06

Skatch


People also ask

How to generate UUID laravel?

We have to simple install webpatser/laravel-uuid package to your laravel application and then a second you can generate uuid. So, you don't have to add aliases or provides etc, just need to install webpatser/laravel-uuid package using composer require command.

What is UUID generator?

A UUID (Universal Unique Identifier) is a 128-bit value used to uniquely identify an object or entity on the internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until A.D. 3400.

Are UUID always unique?

No, a UUID can't be guaranteed to be unique. A UUID is just a 128-bit random number. When my computer generates a UUID, there's no practical way it can prevent your computer or any other device in the universe from generating that same UUID at some time in the future.

What does UUID look like?

Format. In its canonical textual representation, the 16 octets of a UUID are represented as 32 hexadecimal (base-16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens). For example: 123e4567-e89b-12d3-a456-426614174000.


Video Answer


1 Answers

After laravel 5.6 a new helper was added to generate Universal Unique Identifiers (UUID)

use Illuminate\Support\Str;  return (string) Str::uuid();  return (string) Str::orderedUuid(); 

The methods return a Ramsey\Uuid\Uuid object

The orderedUuid() method will generate a timestamp first UUID for easier and more efficient database indexing.

like image 117
Hemerson Varela Avatar answered Oct 12 '22 19:10

Hemerson Varela