定制 uthman/hashid 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2022-03-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固