From d64b9237e9e8f3e4323225b13f85f2a6712445e7 Mon Sep 17 00:00:00 2001 From: Jamie Fairweather <16147285+zagreusinoz@users.noreply.github.com> Date: Wed, 13 Aug 2025 16:46:25 +0100 Subject: [PATCH 1/2] Feature: Added configuration option for uuid version, defaults to uuid4 and if no configuration option is set then still defaults to uuid4. --- config/model-uuid.php | 5 +++++ src/GeneratesUuid.php | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/model-uuid.php b/config/model-uuid.php index b03068f..fb29d30 100644 --- a/config/model-uuid.php +++ b/config/model-uuid.php @@ -5,4 +5,9 @@ * The default column name which should be used to store the generated UUID value. */ 'column_name' => 'uuid', + + /** + * The default uuid version used to generate UUID value. + */ + 'uuid_version' => 'uuid4', ]; diff --git a/src/GeneratesUuid.php b/src/GeneratesUuid.php index a664bd4..9440507 100644 --- a/src/GeneratesUuid.php +++ b/src/GeneratesUuid.php @@ -92,9 +92,9 @@ public function resolveUuid(): UuidInterface return call_user_func([Uuid::class, $this->resolveUuidVersion()]); } - public function uuidVersion(): string + public function uuidVersion(): string | null { - return 'uuid4'; + return null; } /** @@ -102,7 +102,10 @@ public function uuidVersion(): string */ public function resolveUuidVersion(): string { - if (($uuidVersion = $this->uuidVersion()) === 'ordered') { + + $uuidVersion = $this->uuidVersion() ?? config('model-uuid.uuid_version', 'uuid4'); + + if ($uuidVersion === 'ordered') { $uuidVersion = 'uuid6'; } From 419a0a013e9f9379541394789865472e8a9da0fa Mon Sep 17 00:00:00 2001 From: Jamie Fairweather <16147285+zagreusinoz@users.noreply.github.com> Date: Wed, 13 Aug 2025 16:46:25 +0100 Subject: [PATCH 2/2] Feature: Added configuration option for uuid version, defaults to uuid4 and if no configuration option is set then still defaults to uuid4. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index e94f6f0..9229630 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,17 @@ class Post extends Model } ``` +Alternatively, if you would like to use a different `$uuidVersion` on all of your models, then you can set the configuration setting to one of the versions outlined above. If a different `$uuidVersion` is specified on a model then this will override the global setting. Remove the ```public function uuidVersion(): string``` function from your model if it is set to take the global default. + +```php +return [ + /** + * The default uuid version used to generate UUID value. + */ + 'uuid_version' => 'uuid4', +]; +``` + Whilst not recommended, if you _do_ choose to use a UUID as your primary model key (`id`), be sure to configure your model for this setup correctly. Not updating these properties will lead to Laravel attempting to convert your `id` column to an integer, which will be cast to `0`. When used in combination with the `EfficientUuid` cast, this casting will result in a `Ramsey\Uuid\Exception\InvalidUuidStringException` being thrown. ```php