antoninmasek/laravel-model-hashids
Composer 安装命令:
composer require antoninmasek/laravel-model-hashids
包简介
Easily use Hashids with Laravel models.
README 文档
README
In some cases I really like to use Hashids instead of uuids as my model keys. For me Hashids are less awkward to read and the resulting URL looks a bit nicer in my opinion. This package is inspired by laravel-model-uuid by Michael Dyrynda and aims to make it a breeze to start using Hashids as your model keys.
Installation
You can install the package via composer:
composer require antoninmasek/laravel-model-hashids
You can publish the config file with:
php artisan vendor:publish --tag="laravel-model-hashids-config"
This is the contents of the published config file:
return [ /* * The following column will be filled with the generated hash id. If you decide to also bind * models to hash_id, then this column will be used as route key name. */ 'hash_id_column' => 'hash_id', /* * Define the column name, which will be used to generate the hash id. This column has to contain * a numeric value or an array of numbers and should be unique for each model to prevent * potential collisions. */ 'model_key' => 'id', ];
This package uses antoninmasek/laravel-hashids in the background. And if you wish to configure some aspects of the underlying hash id generation, then please take a look at a readme of the package.
Usage
To use this package you'll be most interested in the following two traits: GeneratesHashId and BindsOnHashId.
Generating hash id
In order for your model to automatically get a hash id after it is created just use
GeneratesHashId trait on your model:
use AntoninMasek\Hashids\Traits\GeneratesHashId; class YourModel extend Model { use GeneratesHashId; }
Binding on hash id
To also bind your model to hash id you also need to use BindsOnHashId trait:
use AntoninMasek\Hashids\Traits\GeneratesHashId; use AntoninMasek\Hashids\Traits\BindsOnHashId; class YourModel extend Model { use GeneratesHashId; use BindsOnHashId; }
Configuration
If you need to execute some logic in order to determine salt/alphabet/minlength you have a few callbacks at your disposal:
Global
If you want to set these globally you may use the following callbacks. The callback is supplied with the model as a parameter.
use AntoninMasek\Hashids\ModelHashids; ModelHashids::generateSaltUsing(function(Model $model) { // your logic return $salt; }); ModelHashids::generateMinLengthUsing(function(Model $model) { // your logic return $minLength; }); ModelHashids::generateAlphabetUsing(function(Model $model) { // your logic return $alphabet; });
Local
If you wish to have a specific logic just for a certain model you may define these methods on the desired model:
// Overwrite the column to fill with hash id for this specific model public function hashIdColumn(): string; // Overwrite the column to use for hash id generation for this specific model public function hashIdKeyColumn(): string; // Overwrite the logic to generate salt for this specific model public function hashIdSalt(): string; // Overwrite the logic to generate alphabet for this specific model public function hashIdAlphabet(): string; // Overwrite the logic to generate min length for this specific model public function hashIdMinLength(): string;
Precedence
This is the order in which the values are taken:
- Model specific logic
- Global logic
- Config values
- Hashids package
Regenerating hash id
If you wish to regenerate a hash id for a particular model with current configuration you may do so as follows:
// This will save the new hash id directly to database $model->regenerateHashId(); // This will just regenerate the hash id on the instance without persisting it. // You will need to call the save() method to persist it. $model->regenerateHashId(saveToDatabase: false);
Limitations
If your model key is auto-incrementing then, at least at the moment, there are 2 round-trips to the database. 1st to create the model and receive the ID and then 2nd to set the hash_id based on the ID.
Note:
Updatingeloquent event is not fired, when setting thehash_id. This is the default behaviour since version 0.6.0. It is still possible to change this viasave_quietlyconfig setting.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
Special thanks
- To Michael Dyrynda for his laravel-model-uuid package, by which this package is heavily inspired.
- To Spatie for their amazing skeleton which I used to scaffold this repository.
antoninmasek/laravel-model-hashids 适用场景与选型建议
antoninmasek/laravel-model-hashids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.33k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel-model-hashids」 「antoninmasek」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 antoninmasek/laravel-model-hashids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 antoninmasek/laravel-model-hashids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 antoninmasek/laravel-model-hashids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple Laravel wrapper around Hashids library.
Simple object hydrator
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 7.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-12