Skip to content

Instantly share code, notes, and snippets.

@danb-humaan
Created December 10, 2015 04:00
Show Gist options
  • Save danb-humaan/7cc8e72b19504269b662 to your computer and use it in GitHub Desktop.
Save danb-humaan/7cc8e72b19504269b662 to your computer and use it in GitHub Desktop.
/**
* Binds creating/saving events to create UUIDs (and also prevent them from being overwritten).
*
* @return void
*/
public static function bootUuidModel()
{
static::creating(function ($model) {
// Don't let people provide their own UUIDs, we will generate a proper one.
$model->uuid = Uuid::uuid4()->toString();
});
static::saving(function ($model) {
// What's that, trying to change the UUID huh? Nope, not gonna happen.
$original_uuid = $model->getOriginal('uuid');
if ($original_uuid !== $model->uuid) {
$model->uuid = $original_uuid;
}
});
}
@joelharkes
Copy link

you are probably better of throwing an exception.

if ($original_uuid !== $model->uuid) {
            throw new IdentityIntegrityException()
}

So you will know when your code screws up, instead of never knowing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment