phpvms/app/Models/Traits/HashIdTrait.php

33 lines
609 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
2020-02-23 05:03:01 +08:00
use App\Support\Utils;
2017-08-24 23:51:47 +08:00
trait HashIdTrait
2017-06-25 02:20:24 +08:00
{
/**
* @throws \Hashids\HashidsException
2018-08-27 00:40:04 +08:00
*
* @return string
*/
final protected static function createNewHashId(): string
{
2020-02-23 05:03:01 +08:00
return Utils::generateNewId();
}
/**
* Register callbacks
2018-08-27 00:40:04 +08:00
*
2018-03-09 22:52:55 +08:00
* @throws \Hashids\HashidsException
*/
final protected static function bootHashIdTrait(): void
2017-06-25 02:20:24 +08:00
{
static::creating(function ($model) {
if (empty($model->id)) {
$model->id = static::createNewHashId();
2017-06-25 02:20:24 +08:00
}
});
}
}