Skip to content

Commit

Permalink
Update uuid.md
Browse files Browse the repository at this point in the history
  • Loading branch information
drbyte committed Sep 15, 2020
1 parent ccb22bf commit adbcf78
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions docs/advanced-usage/uuid.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ For this, in the configuration file edit `column_names.model_morph_key`:
### Models
If you want all the role/permission objects to have a UUID instead of an integer, you will need to Extend the default Role and Permission models into your own namespace in order to set some specific properties. (See the Extending section of the docs, where it explains requirements of Extending, as well as the configuration settings you need to update.)

- You may want to set `protected $keyType = 'string';` so Laravel handles joins as strings and doesn't cast to integer.
- You likely want to set `protected $keyType = 'string';` so Laravel handles joins as strings and doesn't cast to integer.
- OPTIONAL: If you changed the field name in your migrations, you must set `protected $primaryKey = 'uuid';` to match.
- Usually for UUID you will also set `public $incrementing = false;`. Remove it if it causes problems for you.

Expand All @@ -96,17 +96,27 @@ It is common to use a trait to handle the $keyType and $incrementing settings, a

trait UuidTrait
{
public $incrementing = false;
protected $keyType = 'string';

protected static function boot()
protected static function bootUuidTrait()
{
parent::boot();

static::creating(function ($model) {
$model->keyType = 'string';
$model->incrementing = false;

$model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid();
});
}

public function getIncrementing()
{
return false;
}

public function getKeyType()
{
return 'string';
}
}
```

Expand All @@ -117,3 +127,22 @@ In the default User model provided with Laravel, this is done by extending anoth
However, your app's UUID implementation may need to override that in order to set some of the properties mentioned in the Models section above.

If you are running into difficulties, you may want to double-check whether your User model is doing UUIDs consistent with other parts of your app.


## REMINDER:

> THIS IS NOT A FULL LESSON ON HOW TO IMPLEMENT UUIDs IN YOUR APP.
Again, since each UUID implementation approach is different, some of these may or may not benefit you. As always, your implementation may vary.



### Packages
There are many packages offering UUID features for Eloquent models. You may want to explore whether these are of value to you in your study of implementing UUID in your applications:

https://github.com/JamesHemery/laravel-uuid
https://github.com/jamesmills/eloquent-uuid
https://github.com/goldspecdigital/laravel-eloquent-uuid
https://github.com/michaeldyrynda/laravel-model-uuid

Remember: always make sure you understand what a package is doing before you use it! If it's doing "more than what you need" then you're adding more complexity to your application, as well as more things to test and support!

0 comments on commit adbcf78

Please sign in to comment.