Skip to content

Instantly share code, notes, and snippets.

@ed-fruty
Last active July 11, 2018 12:05
Show Gist options
  • Save ed-fruty/2a9470ff330e8b7a41b0735ec6977a68 to your computer and use it in GitHub Desktop.
Save ed-fruty/2a9470ff330e8b7a41b0735ec6977a68 to your computer and use it in GitHub Desktop.
Eloquent uuid as primary key
<?php
public static function boot()
{
parent::boot();
$callback = function (Model $model) {
$model->setKeyType('string');
$model->setIncrementing(false);
if (! $model->getKey()) {
$model->{$model->getKeyName()} = \Str::uuid();
}
};
static::creating($callback);
static::retrieved($callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment