uthman/hashid
Composer 安装命令:
composer require uthman/hashid
包简介
Simple package to hash eloquent primary key, Forked from erashdan/hashid
关键字:
README 文档
README
This package hashes the primary key of an eloquent record.
// Get Hash ID $user = \App\User::first(); $user->hashed_id; //x7LR5oQJleJX60yPpNWV
Installation
This package can be used in Laravel 5.5 or higher.
You can install the package via composer:
composer require erashdan/hashid
Laravel's package auto discovery will automatically register the service provider for you.
Then you need to publish the configuration to your project:
php artisan vendor:publish --provider="Erashdan\Hashid\HashidServiceProvider" --tag="config"
And add the key used for the hashing in .env file
HASHID_KEY=SET_YOUR_KEY
OR
Use Laravel's own app key, change the key parameter in config/hashid.php to Laravel's application key
'key' => env('APP_KEY'),
You can also change the length of the resulted hash from .env file.
HASHID_LENGTH=6
Usage
Eloquent by default doesn't implement hashid, so you should use the trait provided from the package.
use Illuminate\Database\Eloquent\Model; use Erashdan\Hashid\Traits\Hashid; class Post extends Model { use Hashid;
You can then use the hashed_id attribute on the eloquent object itself.
$post = \App\Models\Post::first(); $post->hashed_id; //x7LR5oQJleJX60yPpNWV
Or find a resource by hash
$post = \App\Models\Post::FindOrFailHashed('x7LR5oQJleJX60yPpNWV'); $post->id; //1
Validation
You can validate if hashed id is existed in model or not
request()->validate([ 'post_id' => 'hashed_exists:' . \App\Post::class ]);
Testing
composer test
Credits
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2022-03-18