phpvms/app/Models/Traits/HashId.php
2018-03-02 13:12:39 -06:00

32 lines
608 B
PHP

<?php
namespace App\Models\Traits;
use Hashids\Hashids;
trait HashId
{
/**
* @return string
* @throws \Hashids\HashidsException
*/
protected static function createNewHashId()
{
$hashids = new Hashids('', 12);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
}
/**
* Register callbacks
*/
protected static function bootHashId()
{
static::creating(function ($model) {
if (empty($model->id)) {
$model->id = static::createNewHashId();
}
});
}
}