3737
3838## Introduction
3939
40- A simple drop-in solution for providing UUIDv4 support for the IDs of your
40+ A simple drop-in solution for providing UUID support for the IDs of your
4141Eloquent models.
4242
43+ Both ` v1 ` and ` v4 ` IDs are supported out of the box, however should you need
44+ ` v3 ` or ` v5 ` support, you can easily add this in.
45+
4346## Installing
4447
48+ Reference the table below for the correct version to use in conjunction with the
49+ version of Laravel you have installed:
50+
51+ | Laravel | This package |
52+ | ------- | ------------ |
53+ | ` v5.8.* ` | ` v1.* ` |
54+
4555You can install the package via composer:
4656
4757``` bash
48- composer require goldspecdigital/laravel-eloquent-uuid
58+ composer require goldspecdigital/laravel-eloquent-uuid: ~ v1.1
4959```
5060
5161## Usage
@@ -90,8 +100,8 @@ class User extends Authenticatable
90100
91101### Generating UUIDs
92102
93- If you don't specify the value for the primary key of your model, a UUIDv4 will
94- be automatically generated. However, if you do specify your own UUIDv4 then it
103+ If you don't specify the value for the primary key of your model, a UUID will
104+ be automatically generated. However, if you do specify your own UUID then it
95105will not generate one, but instead use the one you have explicitly provided. This
96106can be useful when needing the know the ID of the model before you have created
97107it:
@@ -106,6 +116,56 @@ $model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
106116echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e
107117```
108118
119+ ### Specifying UUID versions
120+
121+ By default, ` v4 ` UUIDs will be used for your models. However, you can also
122+ specify ` v1 ` UUIDs to be used by setting the following property on your model:
123+
124+ ``` php
125+ <?php
126+
127+ namespace App\Models;
128+
129+ use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
130+
131+ class BlogPost extends Model
132+ {
133+ /**
134+ * The UUID version to use.
135+ *
136+ * @var int
137+ */
138+ protected $uuidVersion = 1;
139+ }
140+ ```
141+
142+ #### Support for ` v3 ` and ` v5 `
143+
144+ Should you need support for ` v3 ` or ` v5 ` UUIDs, you can simply override the
145+ method which is responsible for generating the UUIDs:
146+
147+ ``` php
148+ <?php
149+
150+ namespace App\Models;
151+
152+ use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
153+ use Ramsey\Uuid\Uuid;
154+
155+ class BlogPost extends Model
156+ {
157+ /**
158+ * @throws \Exception
159+ * @return string
160+ */
161+ protected function generateUuid(): string
162+ {
163+ // UUIDv3 has been used here, but you can also use UUIDv5.
164+ return Uuid::uuid3(Uuid::NAMESPACE_DNS, 'example.com')->toString();
165+ }
166+ }
167+ ```
168+
109169## Running the tests
110170
111171To run the test suite you can use the following commands:
0 commit comments