Traits
HasUuids
This trait can be added to models to cause them to generate UUID values in specified columns as part of the model creating event.
Usage
Once installed, you can then add this trait to Models that should have a uuid column.
You should create a uuid column as part of migration.
$table->uuid('uuid')->unique();
You should then add a protected $uuid_column property that is the name of column which should generate a uuid. The most likely use-case is:
protected $uuid_column = 'uuid';
Warning
It it not recommended that you add this after you already have a table set up and in-use, especially if the site is already live without it. If you are adding this as an update:
- The table column must also be
->nullable(). - Due to the fact that this is tied to the
creatingevent hook, you will need to create additional steps to ensure that existing items have their uuid columns populated.