phpvms/app/Models/Traits/HashId.php
2017-12-12 13:26:08 -06:00

28 lines
506 B
PHP

<?php
namespace App\Models\Traits;
use Hashids\Hashids;
trait HashId
{
protected static function boot()
{
parent::boot();
static::creating(function ($model)
{
$key = $model->getKeyName();
if (empty($model->{$key})) {
$hashids = new Hashids('', 12);
$mt = str_replace('.', '', microtime(true));
$id = $hashids->encode($mt);
$model->{$key} = $id;
}
});
}
}