m-wardany/hash-ids

此包旨在将顺序键进行散列,使其看起来像随机字符,从而难以预测。在保存之前无需检查键是否已存在于数据库中,因为每个键都是保证唯一的。

v0.1.6 2024-07-22 13:25 UTC

This package is auto-updated.

Last update: 2024-09-22 13:49:47 UTC


README

此包旨在将顺序键进行散列,使其看起来像随机字符,从而难以预测。在保存之前无需检查键是否已存在于数据库中,因为每个键都是保证唯一的。

安装

Composer安装

composer require m-wardany/hash-ids

发布配置

php artisan vendor:publish --provider="MWardany\HashIds\Providers\ServiceProvider"

用法


use Illuminate\Database\Eloquent\Model;
use MWardany\HashIds\Helpers\HashBuilder;
use MWardany\HashIds\Interfaces\Hashable;
use MWardany\HashIds\Traits\HasHashId as TraitsHasHashId;

class Post extends Model implements Hashable
{
    use TraitsHasHashId;

    protected $fillable = [
        'title',
    ];

    function getHashAttributes(): array
    {
        $pattern = config('hashid.hashed_attributed_pattern', '%s_hashed');
        return [
            // return mixed alphabet encrypted key, also text & int available
            'id' => HashBuilder::mixed('post_key')
                ->minLength(5)
                ->prefix('PK-')
                ->suffix(function (self $model) {
                    return $model->owner->isActive ? '-KD' : '-KP';
                })
        ];
    }
}