phpvms/app/Models/UuidTrait.php

22 lines
430 B
PHP
Raw Normal View History

2017-06-25 02:20:24 +08:00
<?php
namespace App\Models;
use Webpatser\Uuid\Uuid;
trait Uuids
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$key = $model->getKeyName();
if (empty($model->{$key})) {
$model->{$key} = Uuid::generate()->string;
}
#$model->{$model->getKeyName()} = Uuid::generate()->string;
});
}
}