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