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 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'; }