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的主要内容,如果未能解决你的问题,请参考以下文章