swiss-devjoy/laravel-easy-hashids
Composer 安装命令:
composer require swiss-devjoy/laravel-easy-hashids
包简介
Easy HashIds for Laravel Eloquent models with Livewire Support
README 文档
README
A lightweight package that adds Hashid support to your Eloquent models. It automatically generates unique hashids for your models and includes Livewire support for only exposing the hashid as key to properly identify a public model.
Why Use HashIds?
- Security by Obscurity: Hide your sequential database IDs from users (although is not an encryption library)
- Predictability Prevention: Avoid exposing information about your data volume
- Performance: Invalid hashids don't lead to unnecessary database fetches (in most cases)
Features
- ✅ Easy integration with any Eloquent model
- ✅ Automatic hashid generation based on model IDs
- ✅ Livewire component support for passing models with hashids
- ✅ Route model binding support
- ✅ Auto generation of different hashids for different models, even if the ID is the same
- ✅ Configurable alphabet and minimum length globally and per model
- ✅ No database migrations needed - works with your existing models
Installation
You can install the package via composer:
composer require swiss-devjoy/laravel-easy-hashids
You can publish the config file with:
php artisan vendor:publish --tag="laravel-easy-hashids-config"
This is the contents of the published config file:
return [ 'default' => [ // Generate a unique alphabet here: https://sqids.org/playground 'alphabet' => env('HASHID_DEFAULT_ALPHABET', 'VCzODgjZNMFaXTfqnhLp84EtHlk7RmiWrScBoPIwK2QGxs1ed35UJ6yAYb0v9u'), 'min_length' => env('HASHID_DEFAULT_MIN_LENGTH', 10), ], 'models' => [ // App\Models\YourModel::class => [ // 'alphabet' => 'kwevdSQOEiT349X5atVrLozGHFWYp87uAUlc0mbPNIJKf1qMshCyg2BD6ZxnjR', // 'min_length' => 10, // ], ], ];
Usage
- Add the
HasHashidandHashidRoutingtraits to any Eloquent model:
use SwissDevjoy\LaravelEasyHashids\HasHashid; use SwissDevjoy\LaravelEasyHashids\HashidRouting; class Product extends Model { use HasHashid; use HashidRouting; }
- Access the hashid in your code:
$product = Product::find(1); echo $product->hashid; // Returns something like "2tFub5I1ge"
- Use route model binding with hashids:
Route::get('/products/{product}', function (Product $product) { return view('products.show', compact('product')); })->name('products.product'); // Generates a URL with the hashid that looks sth like /products/2tFub5I1ge route('products.show', ['product' => $product]);
Configuration
The published config file contains these settings:
return [ 'default' => [ // Generate a unique alphabet here: https://sqids.org/playground 'alphabet' => env('HASHID_DEFAULT_ALPHABET', 'VCzODgjZNMFaXTfqnhLp84EtHlk7RmiWrScBoPIwK2QGxs1ed35UJ6yAYb0v9u'), 'min_length' => env('HASHID_DEFAULT_MIN_LENGTH', 10), ], 'models' => [ // App\Models\YourModel::class => [ // 'alphabet' => 'kwevdSQOEiT349X5atVrLozGHFWYp87uAUlc0mbPNIJKf1qMshCyg2BD6ZxnjR', // 'min_length' => 10, // ], ], ];
Working with Livewire
The package automatically handles Livewire integration. When passing models with the HasHashid trait to Livewire components, their IDs will be converted to hashids:
class BookComponent extends Component { public Book $book; public function render() { return view('components.book'); } }
The model will be automatically serialized with the hashid and reconstituted when needed.
Converting Between IDs and HashIds
You can manually convert between IDs and hashids:
$book = Book::make(); // Convert ID to hashid $hashid = $book->idToHashid(10); // Convert hashid back to ID $id = $book->hashidToId('2tFub5I1ge');
Relationships
The package works seamlessly with Eloquent relationships:
// Author model (uses HasHashid) $author = Author::findByHashidOrFail('uwe14hrgh'); // Book model (uses HasHashid) $book = $author->books()->findByHashid('2tFub5I1ge');
Advanced Configuration
Model-Specific Settings
You can customize the alphabet and minimum hashid length for specific models in the config file:
'models' => [ App\Models\Book::class => [ 'alphabet' => 'xyz123ABC789DEFGHIJKLMNOPQRSTUVWdefghijklmnopqrstuvwab456XYZ', 'min_length' => 12, ], ],
Auto-Generated Alphabets
If no custom configuration is provided, the package will generate a unique alphabet for each model based on the class name. This ensures distinct hashids across different models even for identical database IDs.
For example, User::find(1)->hashid will be different from Product::find(1)->hashid.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
swiss-devjoy/laravel-easy-hashids 适用场景与选型建议
swiss-devjoy/laravel-easy-hashids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.84k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「devjoy.ch」 「laravel-easy-hashids」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 swiss-devjoy/laravel-easy-hashids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swiss-devjoy/laravel-easy-hashids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 swiss-devjoy/laravel-easy-hashids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Optimize your SQLite database for production in Laravel
Put chunks of your Blade template into immutable static cache files
Add Cloudflare-like image transformations to your app
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 1.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-18