|
5 | 5 | - [Eloquent Model Conventions](#eloquent-model-conventions)
|
6 | 6 | - [Table Names](#table-names)
|
7 | 7 | - [Primary Keys](#primary-keys)
|
| 8 | + - [UUID & ULID Keys](#uuid-and-ulid-keys) |
8 | 9 | - [Timestamps](#timestamps)
|
9 | 10 | - [Database Connections](#database-connections)
|
10 | 11 | - [Default Attribute Values](#default-attribute-values)
|
@@ -197,6 +198,69 @@ If your model's primary key is not an integer, you should define a protected `$k
|
197 | 198 |
|
198 | 199 | Eloquent requires each model to have at least one uniquely identifying "ID" that can serve as its primary key. "Composite" primary keys are not supported by Eloquent models. However, you are free to add additional multi-column, unique indexes to your database tables in addition to the table's uniquely identifying primary key.
|
199 | 200 |
|
| 201 | +<a name="uuid-and-ulid-keys"></a> |
| 202 | +### UUID & ULID Keys |
| 203 | + |
| 204 | +Instead of using auto-incrementing integers as your Eloquent model's primary keys, you may choose to use UUIDs instead. UUIDs are universally unique alpha-numeric identifiers that are 36 characters long. |
| 205 | + |
| 206 | +If you would like a model to use a UUID key instead of an auto-incrementing integer key, you may use the `Illuminate\Database\Eloquent\Concerns\HasUuids` trait on the model. Of course, you should ensure that the model has a [UUID equivalent primary key column](/docs/{{version}}/migrations#column-method-uuid): |
| 207 | + |
| 208 | + use Illuminate\Database\Eloquent\Concerns\HasUuids; |
| 209 | + use Illuminate\Database\Eloquent\Model; |
| 210 | + |
| 211 | + class Article extends Model |
| 212 | + { |
| 213 | + use HasUuids; |
| 214 | + |
| 215 | + // ... |
| 216 | + } |
| 217 | + |
| 218 | + $article = Article::create(['title' => 'Traveling to Europe']); |
| 219 | + |
| 220 | + $article->id; // "8f8e8478-9035-4d23-b9a7-62f4d2612ce5" |
| 221 | + |
| 222 | +By default, The `HasUuids` trait will generate ["ordered" UUIDs](/docs/{{version}}/helpers#method-str-ordered-uuid) for your models. These UUIDs are more efficient for indexed database storage because they can be sorted lexicographically. |
| 223 | + |
| 224 | +You can override the UUID generation process for a given model by defining a `newUniqueId` method on the model. In addition, you may specify which columns should receive UUIDs by defining a `uniqueIds` method on the model: |
| 225 | + |
| 226 | + use Ramsey\Uuid\Uuid; |
| 227 | + |
| 228 | + /** |
| 229 | + * Generate a new UUID for the model. |
| 230 | + * |
| 231 | + * @return string |
| 232 | + */ |
| 233 | + public function newUniqueId() |
| 234 | + { |
| 235 | + return (string) Uuid::uuid4(); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Get the columns that should receive a unique identifier. |
| 240 | + * |
| 241 | + * @return array |
| 242 | + */ |
| 243 | + public function uniqueIds() |
| 244 | + { |
| 245 | + return ['id', 'discount_code']; |
| 246 | + } |
| 247 | + |
| 248 | +If you wish, you may choose to utilize "ULIDs" instead of UUIDs. ULIDs are similar to UUIDs; however, they are only 26 characters in length. Like ordered UUIDs, ULIDs are lexicographically sortable for efficient database indexing. To utilize ULIDs, you should use the `Illuminate\Database\Eloquent\Concerns\HasUlids` trait on your model: |
| 249 | + |
| 250 | + use Illuminate\Database\Eloquent\Concerns\HasUlids; |
| 251 | + use Illuminate\Database\Eloquent\Model; |
| 252 | + |
| 253 | + class Article extends Model |
| 254 | + { |
| 255 | + use HasUlids; |
| 256 | + |
| 257 | + // ... |
| 258 | + } |
| 259 | + |
| 260 | + $article = Article::create(['title' => 'Traveling to Asia']); |
| 261 | + |
| 262 | + $article->id; // "01gd4d3tgrrfqeda94gdbtdk5c" |
| 263 | + |
200 | 264 | <a name="timestamps"></a>
|
201 | 265 | ### Timestamps
|
202 | 266 |
|
|
0 commit comments