Created
December 10, 2015 04:00
-
-
Save danb-humaan/7cc8e72b19504269b662 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you are probably better of throwing an exception.
So you will know when your code screws up, instead of never knowing.