php 创建Hashids Observer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 创建Hashids Observer相关的知识,希望对你有一定的参考价值。

<?php

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;

/**
 * See events
 * @href https://laravel.com/docs/5.5/eloquent#events
 * Available metthods: retrieved, creating, created, updating, updated,
 * saving, saved, deleting, deleted, restoring, restored
 */
class HashidsObserver
{
    /**
     * Listen to the created event.
     *
     * @param  Model $model
     * @return void
     */
    public function creating(Model $model)
    {
        // we only create this once
        if (Schema::hasColumn($model->getTable(), 'hashslug') && is_null($model->hashslug)) {
            $timestamp       = time() + $model->count() + 1;
            $str_slug_fqcn   = str_slug_fqcn($model);
            $model->hashslug = hashids($str_slug_fqcn)->encode($timestamp);
        }
    }
}

以上是关于php 创建Hashids Observer的主要内容,如果未能解决你的问题,请参考以下文章

php 为Hashids Wrapper创建一个帮助器

php Hashids Wrapper

php中通过Hashids将整数转化为唯一字符串

hashids 了解

将Long类型转为字母数字组合的jar包---Hashids

openresty && hashids&& redis 生成短链接