phpvms/app/Models/Traits/Uuids.php

28 lines
498 B
PHP
Raw Normal View History

2017-06-25 02:20:24 +08:00
<?php
2017-08-13 01:24:00 +08:00
namespace App\Models\Traits;
2017-06-25 02:20:24 +08:00
2017-08-24 23:51:47 +08:00
use Hashids\Hashids;
2017-06-25 02:20:24 +08:00
trait Uuids
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$key = $model->getKeyName();
2017-08-24 23:51:47 +08:00
2017-06-25 02:20:24 +08:00
if (empty($model->{$key})) {
$hashids = new Hashids('', 10);
$mt = str_replace('.', '', microtime(true));
2017-09-08 04:40:30 +08:00
$id = $hashids->encode($mt);
2017-08-24 23:51:47 +08:00
$model->{$key} = $id;
2017-06-25 02:20:24 +08:00
}
});
}
}