Last active
July 11, 2018 12:05
-
-
Save ed-fruty/2a9470ff330e8b7a41b0735ec6977a68 to your computer and use it in GitHub Desktop.
Eloquent uuid as primary key
This file contains 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
<?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